Rajib Paul
Rajib Paul

Reputation: 3

Move Uploaded File Faied - php

I have these errors compiling php

MOVE UPLOADED FILE FAILED! Array ( [type] => 2 [message] => move_uploaded_file(): Unable to move '/tmp/phpuFeMBl' to 'upload/horaizontal-hilly-evening.jpg' [file] => /var/www/html/directoryUpload/index.php [line] => 10 )

before that I got an error like >>

MOVE UPLOADED FILE FAILED! Array ( [type] => 2 [message] => move_uploaded_file() expects parameter 1 to be string, array given [file] => /var/www/html/directoryUpload/index.php [line] => 9 )


I actually need to upload and show some of my projects into my portfolio site as a practice project.

I have the upload folder just near the php file.

php code

<?php
$count = 0;
$uploads_dir = 'upload/';

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    foreach ($_FILES["files"]["name"] as $key => $name) {
      if (move_uploaded_file($_FILES['files']['tmp_name'][$key], 'upload/'.$name)) {
            $count++;
          echo "<P>FILE UPLOADED TO: $target_file</P>";
       } else {
          echo "<P>MOVE UPLOADED FILE FAILED!</P>";
          print_r(error_get_last());
       }     

    }

}
?>

HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <title>Directory Upload using webkidriectory</title>
    </head>

    <body>
        <div class="wrap">
            <?php
            if ($count > 0) {
                echo "<p class='msg'>{$count} files uploaded</p>\n\n";
            }
            ?>

            <form method="post" enctype="multipart/form-data">
                <input type="file" name="files[]" id="files" multiple="" directory="" webkitdirectory="" mozdirectory="">
                <input class="button" type="submit" value="Upload" />
            </form>

        </div>
    </body>
</html>

Upvotes: 0

Views: 551

Answers (3)

Rajib Paul
Rajib Paul

Reputation: 3

Ok, got the answer, I have just provide enough permission. sudo chmod -R 0777 /tmp and sudo chmod -R 0777 /var/www now one single image is in upload folder but how may I upload a full directory? Anyone please let me have some way.

I actually need to upload and show some of my projects into my portfolio site as a practice project. Some help is really needed!

Thanks

Upvotes: 0

steven
steven

Reputation: 4875

If you run your script for example with apache on a debian server it is executed by a user called www-data and group www-data. In that case you should change the owner of your upload folder to www-data with

chown www-data:www-data uploads

And to be sure that the new owner is allowed to write into you should give him read and write access by

chmod 644 uploads

I suspect that missing write permissons are the reason for your failing move.

Another simple way but less secure is to change the chmod to 777 regardless of the owner. But this means tha any user may read write and execute that folder.

Upvotes: 0

Rushil K. Pachchigar
Rushil K. Pachchigar

Reputation: 1321

HTML

<form action="upload.php" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
</form>

PHP

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
?>

Upvotes: 0

Related Questions