Question:

How come i cannot copy file using this php code?

by  |  earlier

0 LIKES UnLike

$filename = $_FILES["file"]["name"];

$new_filename = "_what shud i put here if want to save the new file into the same folder of my upload.php file_". $filename;

$status = copy($filename, $new_filename) or die("Could not copy file contents");

note: assume i have this uploading code below:

<form enctype="multipart/form-data" action="upload.php" method="POST">

<!-- MAX_FILE_SIZE must precede the file input field -->

<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />

<label for="file">Filename:</label>

<input type="file" name="file" id="file" >

<br >

<input type="submit" name="submit" value="Submit" >

</form>

please help!...

 Tags:

   Report

3 ANSWERS


  1. if ($_POST[&#039;submit&#039;]) {

    move_uploaded_file($_FILES[&#039;file&#039;][&#039;tm... &quot;folder/&quot;.$_FILES[&#039;file&#039;][&#039;name&#039;]);

    }

    &lt;form enctype=&quot;multipart/form-data&quot; action=&quot;upload.php&quot; method=&quot;POST&quot;&gt;

    &lt;!-- MAX_FILE_SIZE must precede the file input field --&gt;

    &lt;input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;1000000&quot; /&gt;

    &lt;label for=&quot;file&quot;&gt;Filename:&lt;/label&gt;

    &lt;input type=&quot;file&quot; name=&quot;file&quot; id=&quot;file&quot; &gt;

    &lt;br &gt;

    &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; &gt;

    &lt;/form&gt;


  2. use move_uploaded_file function to copy the uploaded file safely. Use the tmp_name and name variables inside $_FILES properly.

    Additionally, enable error_reporting and display_errors to see exactly what is going wrong.

    $status = move_uploaded_file(

    $_FILES[&#039;file&#039;][&#039;tmp_name&#039;],

    &quot;relative_destination_path/&quot; . basename( $_FILES[&#039;file&#039;][&#039;name&#039;] )

    );

  3. 1. $new_filename = &quot;way too long!&quot;

    2. Have you changed to the uploadedfiles directory?

    (copy() can copy to another path, but are the permissions for that other path correctly set?

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.