如果未上传文件,则使用/从服务器获取默认图像


Use/get default image from server if no file is uploaded

嗨,我有一个表格要求用户上传照片。 在图像

创建从JPG和图像复制重新采样中使用照片作为第二张图像(图像在另一张图像之上)。 但似乎上传的图像没有得到正确获取。 此外,当没有上传文件时,不会从服务器传递默认图像。 我收到这些错误:

Warning</b>:  imagecreatefromjpeg() [<a href='function.imagecreatefromjpeg'>function.imagecreatefromjpeg</a>]: Filename cannot be empty in <b>/home/content/52/12096052/html/Testupdate/processor.php</b> on line <b>117</b>
Warning</b>:  imagesx() expects parameter 1 to be resource, boolean given in <b>/home/content/52/12096052/html/Testupdate/processor.php</b> on line <b>145</b><br />
Warning</b>:  imagesy() expects parameter 1 to be resource, boolean given in /home/content/52/12096052/html/Testupdate/processor.php</b> on line <b>145</b><br />
<b>Warning</b>:  imagecopyresampled() expects parameter 2 to be resource, boolean given in /home/content/52/12096052/html/Testupdate/processor.php</b> on line <b>145</b><br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/content/52/12096052/html/Testupdate/processor.php:117) in <b>/home/content/52/12096052/html/Testupdate/processor.php</b> on line <b>186</b><br />

这是我的代码处理器.php:

    $con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to 
    connect to database! Please try again later.");
    mysql_select_db($dbname);
    // make a note of the current working directory, relative to root.
    $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
    // make a note of the directory that will recieve the uploaded files //i made it as the same directory where the .php file is
    $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self;
    // make a note of the location of the upload form in case we need it
    $uploadForm = 'https://' . $_SERVER['HTTP_HOST'] . $directory_self . 'index.php';
    // name of the fieldname used for the file in the HTML form
    $fieldname = 'file';
    // check the upload form was actually submitted else print form
    if(isset($_POST['submit']))
    {           
        if($_FILES['file']['tmp_name']!="")
        {
            $allowedExts = array("jpeg", "jpg");
            $temp = explode(".", $_FILES['file']['name']);
            $extension = end($temp);
            $notallowedExts = array("png", "gif", "pdf", "doc", "docx", "txt", "html", "xlsx", "mov");
            $nottemp = explode(".", $_FILES['file']['name']);
            $notextension = end($nottemp);
            if ((($_FILES['file']['type'] == "image/jpeg") || ($_FILES['file']['type'] == "image/jpg")) && ($_FILES['file']['size'] < 1300000) && in_array($extension, $allowedExts))  
            {       
              $now = time();
              while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
              {
                  $now++;
              }
              move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename);
            }
            elseif(($_FILES['file']['size'] > 1300000)||(in_array($notextension, $notallowedExts)))
            {
                echo '<script type="text/javascript">'; 
                echo 'alert("Invalid file upload. Please check your input.");'; 
                echo 'window.location.href = "index.php";';
                echo '</script>';
                die();
            }   
        }
        else
        {
            $uploadFilename = "def_img.jpg";
        }   
    }
    //for textbox input
    $name = $_POST['name'];
    $email = $_POST['email'];
    $office_id = $_POST['office_id'];
    $var_title = $_POST['title'];
    $var_story = $_POST['story'];
    $var_task = $_POST['task'];
    $var_power = $_POST['power'];
    $var_solve = $_POST['solve'];
    $var_result = $_POST['result'];
    $get_title = $_POST['title'];
    $title = strtoupper ($get_title);
    $namehere = "Super Story By " .$_POST['name'];
    $story = "My super story begins with " . $_POST['story'] . " My task was " . $_POST['task'] ." With the super power of ". $_POST['power'] ." I solved it by ". $_POST['solve'] ." The result was ". $_POST['result'];
    if (!empty($name) && !empty($email) && !empty($office_id) && !empty($title) && !empty($var_title) && !empty($var_story) && !empty($var_task) && !empty($var_power) && !empty($var_solve) && !empty($var_result)) {
    header('Content-Type: image/jpeg');
    $imagename = $uploadFilename;
    $im = imagecreatefromjpeg("bg.jpg");
    $img2 = imagecreatefromjpeg($imagename);
    $black = imagecolorallocate($im, 0, 0, 0);
    $font = 'arialbi.ttf';
    $font2 = 'ariali.ttf';
    $newtitle = wordwrap($title, 24, "'n", true);
    $newertitle = explode("'n", $newtitle);
    imagettftext($im, 33, 0, 8, 270, $black, $font, $newertitle[0]);
    imagettftext($im, 33, 0, 8, 320, $black, $font, $newertitle[1]);
    imagettftext($im, 12, 0, 283, 365, $black, $font, $namehere);
    $newtext = wordwrap($story, 50, "'n", true);
    $newertext = explode("'n", $newtext);
    imagettftext($im, 10, 0, 283, 385, $black, $font2, $newertext[0]);
    imagettftext($im, 10, 0, 283, 400, $black, $font2, $newertext[1]);
    imagettftext($im, 10, 0, 283, 415, $black, $font2, $newertext[2]);
    imagettftext($im, 10, 0, 283, 430, $black, $font2, $newertext[3]);
    imagettftext($im, 10, 0, 283, 445, $black, $font2, $newertext[4]);
    imagettftext($im, 10, 0, 283, 460, $black, $font2, $newertext[5]);
    imagettftext($im, 10, 0, 283, 475, $black, $font2, $newertext[6]);
    imagettftext($im, 10, 0, 283, 490, $black, $font2, $newertext[7]);
    imagettftext($im, 10, 0, 283, 505, $black, $font2, $newertext[8]);
    imagettftext($im, 10, 0, 283, 520, $black, $font2, $newertext[9]);
    imagettftext($im, 10, 0, 283, 535, $black, $font2, $newertext[10]);
    imagecopyresampled($im, $img2, 10, 350, 0, 0, 263, 175, imagesx($img2), imagesy($img2));
    $date_created = date("YmdHis");//get date created
    $img_name = "-img_entry.jpg"; //the file name of the generated image
    $img_newname = $date_created . $img_name; //datecreated+name
    $img_dir =dirname($_SERVER['SCRIPT_FILENAME']) ."/". $img_newname; //the location to save the image 
    imagejpeg($im, $img_dir , 80); //function to save the image with the name and quality
    $newpath = "/home/content/52/12096052/html/Testupdate/image_entry/";
    $newdir = $newpath.$img_newname;
    copy ($img_dir, $newdir); 
    $http_dir = 'https://www.uaewebdeveloper.com/Testupdate/image_entry/';
    $post_link = $http_dir . $img_newname;
    $msg = 'Super Story by ';
    $post_msg = $msg.$name;
    imagedestroy($im);
    }
    else{
        echo '<script type="text/javascript">'; 
        echo 'window.alert("*All fields required.");'; 
        echo 'window.location.href = "index.php";';
        echo '</script>';
    }
    //get time to save in db
    $sql_date = date("Y/m/d H:i:s");
    //save inputs to db
    if (!empty($name) && !empty($email) && !empty($office_id) && !empty($title) && !empty($story)) {
        $save_sql = mysql_query("INSERT INTO `tbl_amatest` (filename, name, email, office_id, title, story, time) VALUES ('$img_newname','$name','$email','$office_id','$title','$story','$sql_date')");
        header('Location:' .'success.html'); 
        mysql_close($con);
    }

当我将第二个图像指定为 dafault 时,它可以工作。 但是当有人上传文件时,它只会获得默认值而不是上传。 所以基本上,我在这里有两个问题:

    上传
  1. 的图片未正确传递/上传
  2. 未正确从服务器获取默认映像

请帮助我。 我不知道该怎么办。 我读过帖子,几乎一样,但它对我没有帮助。 提前非常感谢!

尝试在代码中进行两个主要更改:

  1. 替换代码 if(isset($_FILES['file']['tmp_name'])){使用 if($_FILES['file']['tmp_name']!="){

  2. 从默认图像中删除路径:$uploadFilename = 'def_img.jpg';

我已经尝试了它的工作正常。我测试默认图像和上传图像的完整代码都可用于进一步处理。

$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
// make a note of the directory that will recieve the uploaded files //i made it as the same directory where the .php file is
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self;
#echo "$uploadsDirectory<br>";
#exit;
$fieldname = 'file';
if(isset($_POST['submit']))
{           
    if($_FILES['file']['tmp_name']!="")
    {
        $allowedExts = array("jpeg", "jpg");
        $temp = explode(".", $_FILES['file']['name']);
        $extension = end($temp);
        $notallowedExts = array("png", "gif", "pdf", "doc", "docx", "txt", "html", "xlsx", "mov");
        $nottemp = explode(".", $_FILES['file']['name']);
        $notextension = end($nottemp);
        if ((($_FILES['file']['type'] == "image/jpeg") || ($_FILES['file']['type'] == "image/jpg")) && ($_FILES['file']['size'] < 1300000) && in_array($extension, $allowedExts))  
        {       
          $now = time();
          while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
          {
              $now++;
          }
          move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename);
        }
        elseif(($_FILES['file']['size'] > 1300000)||(in_array($notextension, $notallowedExts)))
        {
            echo '<script type="text/javascript">'; 
            echo 'alert("Invalid file upload. Please check your input.");'; 
            echo 'window.location.href = "index.php";';
            echo '</script>';
            die();
        }   
    }
    else
    {
        $uploadFilename = "images.jpg";
    }   
}
/*$get_title = $_POST['title'];
$story = "My super story begins with " . $_POST['story'] . " My task was " . $_POST['task'] ." With the super power of ". $_POST['power'] ." I solved it by ". $_POST['solve'] ." The result was ". $_POST['result'];
if (!empty($name) && !empty($email) && !empty($office_id) && !empty($title) && !empty($var_title) && !empty($var_story) && !empty($var_task) && !empty($var_power) && !empty($var_solve) && !empty($var_result)) {
*/
echo " FILE : $uploadFilename";
//header('Content-Type: image/jpeg');
$upload = $uploadFilename;
$im = imagecreatefromjpeg("bg.jpg");
$img2 = imagecreatefromjpeg($upload);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'arialbi.ttf';
$font2 = 'ariali.ttf';
$newtitle = wordwrap($title, 24, "'n", true);
$newertitle = explode("'n", $newtitle);

imagettftext($im, 33, 0, 8, 270, $black, $font, $newertitle[0]);
imagettftext($im, 33, 0, 8, 320, $black, $font, $newertitle[1]);
imagettftext($im, 12, 0, 283, 365, $black, $font, $namehere);
$newtext = wordwrap($story, 50, "'n", true);
$newertext = explode("'n", $newtext);
imagettftext($im, 10, 0, 283, 385, $black, $font2, $newertext[0]);
imagettftext($im, 10, 0, 283, 400, $black, $font2, $newertext[1]);
imagecopyresampled($im, $img2, 10, 350, 0, 0, 263, 175, imagesx($img2), imagesy($img2));
$date_created = date("YmdHis");//get date created
$img_name = "-img_entry.jpg"; //the file name of the generated image
$img_newname = $date_created . $img_name; //datecreated+name
$img_dir =dirname($_SERVER['SCRIPT_FILENAME']) ."/". $img_newname; //the location to save the image 
imagejpeg($im, $img_dir , 80); //function to save the image with the name and quality
$newpath = "/home/content/52/12096052/html/Testupdate/image_entry/";
$newdir = $newpath.$img_newname;
copy ($img_dir, $newdir); //copy to new folder

$http_dir = 'https://www.uaewebdeveloper.com/Testupdate/image_entry/';
$post_link = $http_dir . $img_newname;
$msg = 'Super Story by ';
$post_msg = $msg.$name;
imagedestroy($im);
//}