2017-03-27 1 views
2

Hier endet mein Script für das Hochladen ...Wie für kein Bild zu überprüfen und Prozess fortsetzen, ohne

<?php 
session_start(); 
//Create Post 
include_once '../../config/database_connection.php'; 
include_once '../module/selectUser.php'; 
    if (isset($_POST['post'])) { 
if(!isset($_SESSION['user_id'])) { 
    header("Location: ../../login404"); 
} 
//Grab Image Data 
$image_fieldname = "uploadphoto"; 

//Data Request 
$from = $_REQUEST['from']; 
$view = $_REQUEST['view']; 

//Function definations 
function MakeUrls($status) 
{ 
    $find=array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`si','`((?<!//)(www\.\S+[[:alnum:]]/?))`si'); 
    $replace=array('<a href="$1" target="_blank">$1</a>','<a href="'. BASE_URL .'php/module/recordLink.php?link=$1" target="_blank">$1</a>'); 
    return preg_replace($find,$replace,$status); 
} 
//Function testing 
$status = preg_replace("/[\r\n]+/", "<br>", $_REQUEST['status']); 
//Make Url clickable 
$status=MakeUrls($status); 
$status=preg_replace('/#(\\w+)/','<a href="'. BASE_URL .'php/hash/index?hash=$1">$0</a>',$status); 
//Check for Mentions 
$status=preg_replace('/(@\w+)/','<a href="'. BASE_URL .'php/mention?username=$1">$0</a>',$status); 

//Reg ErrorException 
$status = str_replace('<script>', '',$status); 
$status = str_replace('</script>', '',$status); 
$status = str_replace('<p>', '',$status); 
$status = str_replace('</p>', '',$status); 
$status = str_replace('<style>', '',$status); 
$status = str_replace('</style>', '',$status); 
$likes = 0; 


// Potential PHP upload errors 
$php_errors = array(1 => 'Maximum file size in php.ini exceeded', 
     2 => 'Maximum file size in HTML form exceeded', 
     3 => 'Only part of the file was uploaded', 
     4 => 'No file was selected to upload.'); 

// Make sure we didn't have an error uploading the image 
($_FILES[$image_fieldname]['error'] == 0) 
or handle_error("the server couldn't upload the image you selected.", 
$php_errors[$_FILES[$image_fieldname]['error']]); 

// Is this file the result of a valid upload? 
@is_uploaded_file($_FILES[$image_fieldname]['tmp_name']) 
or handle_error("you were trying to do something naughty. Shame on you!", 
"Uploaded request: file named " . 
"'{$_FILES[$image_fieldname]['tmp_name']}'"); 

// Is this actually an image? 
@getimagesize($_FILES[$image_fieldname]['tmp_name']) 
or handle_error("you selected a file for your picture " . 
"that isn't an image.", 
"{$_FILES[$image_fieldname]['tmp_name']} " . 
"isn't a valid image file."); 

// Name the file uniquely 
$now = time(); 
while (file_exists($upload_filename = $upload_dir . $now . 
'-' . 
$_FILES[$image_fieldname]['name'])) { 
$now++; 
} 

// Insert the image into the images table 
$image = $_FILES[$image_fieldname]; 
$image_filename = "Circlepanda-image" . $image['name']; 
$image_info = getimagesize($image['tmp_name']); 
$image_mime_type = $image_info['mime']; 
$image_size = $image['size']; 
$image_data = file_get_contents($image['tmp_name']); 

//Initial Image Upload Commented Out 
$insert_image_sql = sprintf("INSERT INTO post_img " . 
"(filename, mime_type, file_size, image_data, user_id) " . 
"VALUES ('%s', '%s', '%d', '%s', '%s');", 
mysqli_real_escape_string($conn, $image_filename), 
mysqli_real_escape_string($conn, $image_mime_type), 
mysqli_real_escape_string($conn, $image_size), 
mysqli_real_escape_string($conn, $image_data), 
mysqli_real_escape_string($conn, $user_id)); 
mysqli_query($conn, $insert_image_sql) 
or handle_error("Could'nt upload image to our database" 
, mysql_error($conn)); 


$insert_sql = sprintf("INSERT INTO user_post (fullname, view, status, date, likes, user_id, profile_pic_id, image_id) " . 
      "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', %d); ", 
      mysqli_real_escape_string($conn, $fullname), 
      mysqli_real_escape_string($conn, $view), 
      mysqli_real_escape_string($conn, $status), 
      mysqli_real_escape_string($conn, $date), 
      mysqli_real_escape_string($conn, $likes), 
      mysqli_real_escape_string($conn, $user_id), 
      mysqli_real_escape_string($conn, $profile_pic_id), 
      mysqli_insert_id($conn)); 

// Insert the Post into the database 
if (mysqli_query($conn, $insert_sql)) { 
    $_SESSION['msg'] = "Your status was successfully updated"; 
    header("Location: " . $_SERVER['HTTP_REFERER']); 
} else { 
    $_SESSION['msg'] = "Unsuccessful status update"; 
    header("Location: " . $_SERVER['HTTP_REFERER']); 
} 
} 
?> 

Kein Zweifel, es funktioniert gut, aber in einer Situation, wo kein Bild, um es hochgeladen wird ein senden Fehlerseite. Aber was ich wirklich will, ist, Bild-Upload-Skript zu überspringen und nur Benutzerstatus hochladen. Ich weiß nicht, wie ich das machen soll. :(

+0

Welche Version von PHP haben Sie? Seit PHP 5.3.0 können Sie goto http verwenden : //php.net/manual/en/control-structures.goto.php um zu springen (mit einigen Einschränkungen) –

Antwort

0

Ich habe gerade eine Antwort von irgendwo ringsum stack overflow ... Danke Jungs für die Hilfe ... Beantworten Sie unten ... Nochmals vielen Dank.

if ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0) { 
    #cover_image is empty (and not an error) 
} 
0

Sie können einen Standardwert als null für Ihre image Spalte in der Datenbank festgelegt und dann prüfen, ob Sie eine image für Beispiel haben:

if (you have a image) { 
    // render sql query with image 
} else { 
    // if you don't have render a query without image 
} 
then excute your query 
+0

Bitte beachten Sie, dass ich das Bild anders anfordere, $ image_fieldname = "uploadphoto"; Und wenn ich eine if oder sonst Anweisung verwende, wird es immer wahr sein, weil PHP es als String sehen würde ... Also bitte, ich brauche etwas Besseres als if() {} else {}. Vielen Dank! –

Verwandte Themen