Skip to content
Advertisement

File upload does not work in PHP on Apache Server

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?

Advertisement

Answer

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";
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement