1

I have an assignment for school, and I'm not sure how the teacher wants us to accomplish a task.

  1. We need to get an uploaded file as a temp file only (index.php)
  2. Output size of file (upload.php)
  3. User can confirm save of file or not (upload.php)

So, I have the majority down, but my problem lies with creating the temp file into a permanent file.

index.php

<html>
<form action="http://mysite.org/~me/upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br />
<input type="submit" value="Now upload it!">
</form>
</html>

upload.php

<?php
if (($_FILES["file"]["type"] == "application/vnd.ms-excel"))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

    }
    }
  ?>
  <form action="" method="POST">
  <input type="submit" value"YES please save">
  <form>
  <?php
  if (isset($_POST['submit']))
  {
    //Code for saving file
    echo 'File saved!';
  }
  ?>

Is it possible to go about it this way? My last echo statement does not work, so I'm doubtful the file save would be as well.

5
  • 2
    What on earth does this have to do with JavaScript? Also, isset($_POST['submit']) will never be set with the code you're showing. Commented Mar 30, 2012 at 17:35
  • 3
    $_POST only contains keys from elements which have a name attribute set. Commented Mar 30, 2012 at 17:36
  • As for saving the file, have a look at php.net/move_uploaded_file Commented Mar 30, 2012 at 17:37
  • I'm under the impression that JavaScript would be a better method to handle this. That is why I think it has a somewhat relation to JS. Commented Mar 30, 2012 at 17:39
  • 2
    I know this is incomplete, but you do have <form> opening twice. Make sure to use </form> for the latter one. Commented Mar 30, 2012 at 17:39

4 Answers 4

1

Hopefully the following comments can help you with the part you are stuck on.

In case you hadn't realized it already, any files uploaded with PHP are deleted once the PHP request that handled the uploaded file terminates. This means, if you don't do anything with the temp file from the upload, it will be deleted when the PHP script terminates.

One function of interest to you will be move_uploaded_file() which will move the temporary file from the upload to a permanent location of your choice.

Since the file will be uploaded and then you have to display the size and ask the user to confirm the upload, you will have to move the temp file to a permanent temporary location where it is kept when the user hasn't confirmed they want to keep the upload.

I'm not sure if you have been introduced to sessions yet, but if not, you will probably need some hidden form element that will keep track of what file they uploaded, otherwise you can keep this info in the session.

Then when the person submits the form saying they want to keep the file, you can move it again to a permanent location, or if they say no, then delete the file. The problem is, if they never say yes or no, then the file remains on the system.

Hope that helps.

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

2 Comments

"This means, if you don't do anything with the temp file from the upload, it will be deleted when the PHP script terminates." Does that mean when the current php script ends? What happens if the php script ends and another opens on the same page?
Yes, when the current script ends. The "current script" being the one that the upload was sent to. Even though it generates HTML markup that is returned to the browser, once the user sees that HTML in the browser, the request is terminated and the file has already been deleted.
0

Yep, this should work. Your if statements will catch the form submission and then echo your string there. A few little errors in your markup:

<input type="submit" value"YES please save">

Should be

<input type="submit" value="YES please save" name="submit">

3 Comments

True, but irrelevant to the question. OP asks how he can upload the file and then have the user confirm saving of the file afterwards.
I'm not going to do his homework for him. :/ I just gave him a little nugget of information to fix/unstick his current code and told him that yes, it can be done.
Thanks Matt. I was just looking to see if my code would execute, which it did now. Thanks again.
0

Your final if statement in PHP is looking for a post variable named 'submit' but your <input type="submit"> tag has no name.

Comments

0

The file is saved to a temporary location when the upload completes. You can access this temporary file with $_FILES['file]['tmp_name'] BUT the file will be removed at the end of the request if you do nothing about it. This means that when the user clicks YES please save button, the file will not be available any more.

This means that you have to save the file to a disk in the first place, when you first call the upload.php file. There is no way to keep the file "in memory" while the user decides whether or not to save the file permanently.

2 Comments

Are you sure the file wouldn't save if I put the necessary information in there?
Just tried it. Seems like you are correct. A file upload is not possible using this method.

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.