2017-03-27 3 views
2

を終了させずに処理を継続するにはアップロードのための私のスクリプトです...どのなしの画像をチェックして、ここで

<?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']); 
} 
} 
?> 

間違いなく、それが正常に動作しますが、ない画像がアップロードされていない状況で、それは私を送りますエラーページ。しかし、私が本当に望んでいるのは、画像のアップロードスクリプトをスキップし、ユーザーの状態だけをアップロードすることです。私はこの仕事をどうやって作るのか分かりません。あなたがデータベースにあなたのimage列のヌルとしてデフォルト値を設定し、あなたがimageたとえば持っているかどうかを確認することができます

+0

PHPはあなたが後藤HTTPを使用することができます5.3.0ので、あなたは?PHPのバージョンは何がありますか://php.net/manual/en/control-structures.goto.php先をスキップする(ある制限付きで) –

答えて

0

を私はどこから答えを見つけましたスタックオーバーフローの周り...助けてくれてありがとうみんなが...もう一度...以下の回答に感謝。

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

:(:

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

画像を別の方法で要求しています。 $ image_fieldname = "uploadphoto"; if文またはelse文を使用すると、phpが文字列として表示されるため、常に真実になります。 if(){} else {}よりも優れたものが必要です。ありがとう! –

関連する問題