0

i'm trying to send an image through a upload form to a php script but the file doesn't show in my temporary folder.

I've tried searching for the error and came upon this answer. After checking each of these steps several times I still find myself with no uploaded file...

The form :

<form id="addImageForm" action="functions/add_image.php" method="POST" enctype="multipart/form-data">
  <label for="articleImage">Select a file:</label>
  <input type="file" name="articleImage" id="articleImage">
  <input type="submit" class="btn btn-success" value="Submit">
</form>

The add_image.php script :

<?php

/* Checking request parameters */
if(!isset($_FILES['articleImage']) || empty($_FILES['articleImage'])) {
    echo "Error : the file is missing";
    die();
}

/* Checking upload errors */
if($_FILES['articleImage']['error'] != UPLOAD_ERR_OK) {
    echo "Error : ".$_FILES['articleImage']['error'];
    die();
}

/* Checking file size */
if($_FILES['articleImage']['size'] == 0) {
    echo "Error : the file is empty";
    die();
}

/* Checking file has been uploaded to tmp */
if(is_uploaded_file($_FILES['articleImage']['tmp_name'])) {
    echo "Error : the file didn't upload";
    die();
}

/* Checking file doesn't already exist */
$imagePath = "img/articles/".$_FILES['articleImage']['name'];
if(file_exists($imagePath)) {
    echo "Error : the filename is already taken";
    die();
}

/* Writing image */
if(!move_uploaded_file($_FILES['articleImage']['tmp_name'], $imagePath)) {
    echo "Error : the file coundn't be written";
    die();
}

echo 'Success';
die();

?>

The request is correctly sent :


-----------------------------45933353531894573431775954628
Content-Disposition: form-data; name="articleImage"; filename="my_file.png"
Content-Type: image/png

[image content]

-----------------------------45933353531894573431775954628--

But I still get the "file didn't upload" error...

As I mentionned earlier, I did check my php conf (edited the correct php.ini file with /tmp as upload_tmp_dir and set upload limits to 100M), and /tmp has 777 chmod on it.

What am I missing ?

kewlashu
  • 1,099
  • 10
  • 18
Moonspeak
  • 13
  • 4
  • Did you check with $imagePath = $_SERVER["DOCUMENT_ROOT"] . " img/articles/".$_FILES['articleImage']['name']; Also check that $_FILES['articleImage']['name' folder is created before upload use mkdir etc – Fatih Şennik Sep 05 '20 at 15:07
  • what do you see in `var_dump($_FILES)` – kewlashu Sep 05 '20 at 15:53
  • 1
    @FatihŞennik Indeed it was a folder issue, the path wasn't the right one. Thank you for your input ! – Moonspeak Sep 05 '20 at 16:21

1 Answers1

-1

I've copied and somehow tested your code. You don't need to perform is_uploaded_file call, because these checks are made in move_uploaded_file function (https://www.php.net/manual/en/function.is-uploaded-file.php#113766). Also check if directory where you store images exists.

<?php

/* Directory where uploaded images will be placed */
$imageDir = 'img/articles';

/* Checking request parameters */
if (!isset($_FILES['articleImage']) || empty($_FILES['articleImage'])) {
    echo "Error : the file is missing";
    die();
}

/* Checking upload errors */
if ($_FILES['articleImage']['error'] != UPLOAD_ERR_OK) {
    echo "Error : " . $_FILES['articleImage']['error'];
    die();
}

/* Checking file size */
if ($_FILES['articleImage']['size'] == 0) {
    echo "Error : the file is empty";
    die();
}
/* Checking if directory exists */
if (!is_dir($imageDir)) {
    echo 'Image directory does not exist';
    die();
}

/* Checking file doesn't already exist */
$imagePath = $imageDir . $_FILES['articleImage']['name'];
if (file_exists($imagePath)) {
    echo "Error : the filename is already taken";
    die();
}

/* Writing image */
if (!move_uploaded_file($_FILES['articleImage']['tmp_name'], $imagePath)) {
    echo "Error : the file coundn't be written";
    die();
}

echo 'Success';
die();
Sunfline
  • 139
  • 2
  • 8
  • Yup, you were right, it was a directory issue, I had to add the absolute path to make this work. So, oops, rookie mistake ! Thanks a lot ! – Moonspeak Sep 05 '20 at 16:15