0

I'm trying to upload an image file onto a Web server running on a Raspberry. The image should be uploaded through a form and forwarded to a PHP script. I checked if the folders are set to the proper permissions and it seems OK. Code seems to run without a problem.

Here is the form:

<form action="./php/fileUpload.php" method="POST" enctype="multipart/form-data"> 
   Wähle eine Datei aus: <br> <input name="datei" type="file" />
   <input type="submit" value="Sende Datei" />
</form>

The file selected in this form will be sent to this script:

<?php
$upload_dir = "/uploads/";
$file_extension = strtolower(pathinfo($_FILES['datei']['name'], PATHINFO_EXTENSION));
$file_filename = pathinfo($_FILES['datei']['name'], PATHINFO_FILENAME);
$targeted_Upload = $upload_dir . $file_filename . "." . $file_extension;
$temp_file = $_FILES["datei"]["tmp_name"];

var_dump($upload_dir);
echo "<br>";
var_dump($targeted_Upload);
echo "<br>";
var_dump($file_filename);
echo "<br>";
var_dump($file_extension);
echo "<br>";
var_dump($temp_file);
echo "<br>";

if(file_exists($targeted_Upload)){
    echo "file exists... renaming";
    echo "<br>";
    $newName = $upload_dir . $file_filename;
    do{
        $newName = $newName . "_1";
    }while(file_exists(($newName . "." . $file_extension)));

    $targeted_Upload = $newName . "." . $file_extension;

    echo "file was renamed to " . $targeted_Upload;
    echo "<br>";
    var_dump($targeted_Upload);
    echo "<br>";
}

if ($_FILES["datei"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
}

echo"$targeted_Upload";
echo "<br>";

if(move_uploaded_file($temp_file, $targeted_Upload)){
    echo "<br>File was Uploaded" . "<a href=\"$targeted_Upload\">$targeted_Upload</a>";
}else{
    echo "I have no clue what happened";
}
?>

When I execute the script the output on the page is as following

string(9) "/uploads/"
string(16) "/uploads/ken.jpg"
string(3) "ken"
string(3) "jpg"
string(14) "/tmp/phpv0JAu1"
/uploads/ken.jpg
I have no clue what happened

Does anyone have a clue what I did wrong or what went wrong?

10
  • 1
    Does the web server user have permissions to write to /uploads ? Commented Aug 12, 2019 at 12:13
  • "drwxrwxrwx 2 www-data www-data 4096 Jul 17 15:14 uploads" yes. Commented Aug 12, 2019 at 12:16
  • yes you have all the permissions needed for storing document in the folder. actually more than required Commented Aug 12, 2019 at 12:21
  • Yeah i know that. I did that on purpose so that I don't have to watch the permissions for now. Commented Aug 12, 2019 at 12:22
  • if this line is false then something really wrong in the file path- if(move_uploaded_file($temp_file, $targeted_Upload)){ Do one thing just hard-code file path for testing and check Commented Aug 12, 2019 at 12:22

4 Answers 4

1

I had permission issue to the target folder(In case the path is correct).If anyone by chance having this issue, please give permission to the folder using the below command in the terminal.

sudo chmod 777 /path/to/upload/folder

Please note I am using Ubuntu 18.04LTS.

Sign up to request clarification or add additional context in comments.

Comments

0

File will be stored in temporary location- Give full path of the upload location.

if (move_uploaded_file($temp_file, $_SERVER['DOCUMENT_ROOT']/project_name/$targeted_Upload)) {
    echo "Uploaded";
} else {
   echo "File was not uploaded";
}

2 Comments

It really seems to be a problem with the path. I will see what i have to change. Thanks for the help.
okay changing the relative path to the absolute did the trick
0

I think there is problem with your upload path. if PHP script and uploads is in the same directrory then you should use dot[.] for current directory

$upload_dir = "./uploads/"; 

You can check upload error.

 if(move_uploaded_file($temp_file, $targeted_Upload)){
    echo "<br>File was Uploaded" . "<a href=\"$targeted_Upload\">$targeted_Upload</a>";
}else{
    echo "Not uploaded because of error #".$_FILES["datei"]["error"];
}

UPLOAD_ERR_INI_SIZE = Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

UPLOAD_ERR_FORM_SIZE = Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

UPLOAD_ERR_PARTIAL = Value: 3; The uploaded file was only partially uploaded.

UPLOAD_ERR_NO_FILE = Value: 4; No file was uploaded.

UPLOAD_ERR_NO_TMP_DIR = Value: 6; Missing a temporary folder. Introduced in PHP 5.0.3.

UPLOAD_ERR_CANT_WRITE = Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.

UPLOAD_ERR_EXTENSION = Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help.

1 Comment

Yup had to use the absolute path
0

You need to do two things:

  1. Use the absolute file path in the destination parameter in move_uploaded_file().

    Edit move_uploaded_file and append the destination parameter with __DIR__.'/'.: if(move_uploaded_file($temp_file, __DIR__.'/'.$targeted_Upload)){ ... }

  2. Make sure that the directory /uploads is created, and that the web server user have permissions to write to it.

1 Comment

as i said to the others, it really was the absolute path that was missing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.