0

I have been going round in circles with this image upload where when I submit with file selected it uploads and posts directory as expected to database, but of course if I submit without image selected then I get an error.

So I then would would do a check so see if file input is empty however it then stops the upload even when a file is selected and if I remove it I get this error

Warning: getimagesize(): Filename cannot be empty in ....

On searching this issue and see different peoples solutions, they suggest to increase the Upload max file etc... on the wamp php.ini which I did so, and restarted and still the same issue. However there is no issue with the file size as 5.83kb and php.ini is 25mb nor is there an issue with the file type because it does upload if I remove my error check.

Either way I have been scratching my head as I can not get to confirm when empty and echo out the error that I have set and then when file is selected to post. It just gives me the above error when checking.

Below is a working version which posts as expected but displays the error if empty. I don't want it to display this error, I want to display my own error.

Any suggestions? It's driving me up the wall :(

<?php

//UPLOAD IMAGE
if(isset($_POST["UploadImage"])) {

   
  if(is_array($_FILES)) {
  $file = $_FILES['file']['tmp_name'];
        $sourceProperties = getimagesize($file);
        $fileNewName = time();
        $folderPath = "../userImages/";
        $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        $imageType = $sourceProperties[2];
        $resized = "_resized";
     $line = "_"; 
     $original = "_original"; 
  
        switch ($imageType) {


            case IMAGETYPE_PNG:
                $imageResourceId = imagecreatefrompng($file); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagepng($targetLayer,$folderPath.$RegID.$line.$fileNewName.$resized. ".".$ext);
                break;

    
            case IMAGETYPE_GIF:
                $imageResourceId = imagecreatefromgif($file); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagegif($targetLayer,$folderPath.$RegID.$line.$fileNewName.$resized. ".".$ext);
                break;


            case IMAGETYPE_JPEG:
                $imageResourceId = imagecreatefromjpeg($file); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagejpeg($targetLayer,$folderPath.$RegID.$line.$fileNewName.$resized. ".".$ext);
                break;


            default:
                $msg = "<div class=\"alert alert-danger\">Wrong File Format - Only JPG, PNG or GIF - Max 2 MB</div>";
                exit;
                break;
        }
$file = @imagecreatefromjpeg($folderPath.$RegID.$line.$fileNewName.$resized. ".".$ext);
if (!$file)
{
$file= imagecreatefromstring(file_get_contents($folderPath.$RegID.$line.$fileNewName.$resized. ".".$ext));
}
echo "";
         //If I want the Orignal image upload also then remove below comment
        //move_uploaded_file($file, $folderPath.$RegID.$line.$fileNewName.$original. ".".$ext);

  //POST TO DATABASE
  $RegID;
  $RegPhoto = $folderPath.$RegID.$line.$fileNewName.$resized. ".".$ext;
  $sql = "UPDATE registered SET RegID = ?, RegPhoto = ? WHERE RegID = '$RegID'";
  $stmt = $connect->prepare($sql);
  $stmt->bind_param('is', $RegID, $RegPhoto );
  $stmt->execute();
        $msg = "<div class=\"alert alert-success\">Updated Profile Successfully</div>";
    }
}
function imageResize($imageResourceId,$width,$height) {
    $targetWidth =220;
    $targetHeight =200;
    $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);
    imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);
    return $targetLayer;
}
?>
CwStrange
  • 35
  • 7
  • do a quick check on `$_FILES['file']['error']` and reference with http://php.net/manual/en/features.file-upload.errors.php, for example `if($_FILES['file']['error'] == 0) // all is good` – Dale Jan 30 '18 at 14:35
  • You are not properly [checking if the file was uploaded](https://stackoverflow.com/a/946432/1022914). – Mikey Jan 30 '18 at 14:35
  • Thanks for the info guys I will have a read implement addition checks :) – CwStrange Jan 30 '18 at 14:53

0 Answers0