Juliette Juliette - 4 months ago 9
PHP Question

PHP upload not working, no error

I have a site where a user can upload an image (which worked perfectly a few days ago and I didn't even change anything related to the upload)

The file ownership is set to www-data and I also tried with permission 777, so that shouldn't be the reason it's not working.

enctype/data etc are also set

This is the code:

switch ($_FILES['upload']['error']) {
case UPLOAD_ERR_OK:
$msg="There is no error, the file uploaded with success.";
break;
case UPLOAD_ERR_INI_SIZE:
$msg="The uploaded file exceeds the upload_max_filesize directive in php.ini.";
break;
case UPLOAD_ERR_FORM_SIZE:
$msg="The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
break;
case UPLOAD_ERR_PARTIAL:
$msg="The uploaded file was only partially uploaded.";
break;
case UPLOAD_ERR_NO_FILE:
$msg="No file was uploaded.";
break;
case UPLOAD_ERR_NO_TMP_DIR:
$msg="Missing a temporary folder. Introduced in PHP 5.0.3.";
break;
case UPLOAD_ERR_CANT_WRITE:
$msg="Failed to write file to disk. Introduced in PHP 5.1.0.";
break;
default:
$msg="unknown error with upload";
}
echo("<script>console.log('".$msg."'); </script>");


//process image
if($_FILES['upload']['tmp_name']!=null){
$imgPath="../assets/img/horses/horse".$id.".jpg";
if((bool)getimagesize($_FILES["upload"]["tmp_name"])){
$writeimage = fopen($imgPath, "w");
if(!move_uploaded_file($_FILES["upload"]["tmp_name"], $imgPath)){
echo("<script>console.log('couldn't move img');</script>");
} else{
echo("<script>console.log('image uploaded');</script>");
}
fclose($writeimage);
} else{
echo(" <div class='alert alert-warning' id='notSaved' style='margin-top:20px;'>
Fehler: Die Datei die hochgeladen werden soll, scheint kein Bild zu sein.
</div>");
}
}


The output is

"There is no error, the file uploaded with success.

image uploaded."

but the file doesn't change/appear on the server.

Also I can probably delete the lines $writeimage = fopen($imgPath, "w"); and fclose($writeImage), can't I?

Answer

In your code, I can see some issue.

//process image
if($_FILES['upload']['tmp_name']!=null){
    $image="../assets/img/horses/horse".$id.".jpg";
    if((bool)getimagesize($_FILES["upload"]["tmp_name"])){
        $writeimage = fopen($imgPath, "w");
        if(move_uploaded_file($_FILES["upload"]["tmp_name"], $imgPath)){
            echo("<script>console.log('couldn't move img');</script>");
        } else{
            echo("<script>console.log('image uploaded');</script>");
        }
        fclose($writeimage);
    } else{
        echo("  <div class='alert alert-warning' id='notSaved' style='margin-top:20px;'>
                    Fehler: Die Datei die hochgeladen werden soll, scheint kein Bild zu sein.
                </div>");
    }
}

Please check where is $imgPath variable is defined? it should be $image. No need to use fopen and fclose

So Your cose shouled be...

if($_FILES['upload']['tmp_name']!=null){
    $image="../assets/img/horses/horse".$id.".jpg";
    if((bool)getimagesize($_FILES["upload"]["tmp_name"])){
        if(move_uploaded_file($_FILES["upload"]["tmp_name"], $image)){
            echo("<script>console.log('couldn't move img');</script>");
        } else{
            echo("<script>console.log('image uploaded');</script>");
        }
    } else{
        echo("  <div class='alert alert-warning' id='notSaved' style='margin-top:20px;'>
                    Fehler: Die Datei die hochgeladen werden soll, scheint kein Bild zu sein.
                </div>");
    }
}
Comments