Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move_uploaded_file() showing successful but not working

I am using php to upload and move a file to a desired location... while using move_uploaded_file, it says that the file has moved successfully but the file does not show up in the directory. THE HTML AND PHP CODE IS BELOW...

 <form action="test_upload.php" method="POST" enctype="multipart/form-data">
<fieldset>
<label for="test_pic">Testing Picture</label>
<input type="file" name="test_pic" size="30" /><br />
</fieldset>

<fieldset>

<input type="submit" value="submit" />

</fieldset>
</form>

THe php goes like :

    <?php


$image_fieldname = "test_pic";
$upload_dir = "/vidit";
$display_message ='none';

if(move_uploaded_file($_FILES[$image_fieldname]['tmp_name'],$upload_dir) && is_writable($upload_dir)){
    $display_message = "file moved successfully";
    }
    else{
        $display_message = " STILL DID NOT MOVE";
            }



?>

when i run this page and upload a legitimate file - the test_upload.php echoes file uploaded successfully. but when i head on to the folder "vidit" in the root of the web page. the folder is empty...

I am using wamp server .

like image 952
valiantb Avatar asked Sep 30 '12 06:09

valiantb


People also ask

What does Move_uploaded_file return?

The move_uploaded_file() function moves an uploaded file to a new destination. Note: This function only works on files uploaded via PHP's HTTP POST upload mechanism. Note: If the destination file already exists, it will be overwritten.

How to use move_ uploaded_ file function in PHP?

PHP - Function move_uploaded_file() The move_uploaded_file() function can move an uploaded file to new location. If the filename is not a valid upload file, then no action can occur and return false. If the filename is a valid upload file but can't be moved for some reason, then no action can occur and return false.

How to get uploaded file path in PHP?

The $_FILES is the by default keyword in PHP to access the details of files that we uploaded. The file refers to the name which is defined in the “index. html” form in the input of the file.

Which information is stored by $_ files in PHP?

PHP $_FILES The global predefined variable $_FILES is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data. $_FILES['file']['name'] - The original name of the file to be uploaded.


1 Answers

You need to append filename into your destination path. Try as below

$doc_path = realpath(dirname(__FILE__));

if(move_uploaded_file($_FILES[$image_fieldname]['tmp_name'],$doc_path.$upload_dir.'/'.$_FILES[$image_fieldname]['name']) && is_writable($upload_dir)){
    $display_message = "file moved successfully";
}
else{
    $display_message = " STILL DID NOT MOVE";
}

See PHP Manual for reference. http://php.net/manual/en/function.move-uploaded-file.php

like image 72
GBD Avatar answered Sep 28 '22 07:09

GBD