1 Replies - 1269 Views - Last Post: 22 April 2013 - 07:07 PM Rate Topic: -----

#1 webrider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-April 13

How to save file to a session variable in PHP uploading form?

Posted 22 April 2013 - 06:42 PM

I can already upload file using a from, but it's just that when I put some confirmation prompt, the upload command is not working. It only works by only one click of button. I can't even put the upload commands in a function because it also loses its data. I tried saving it using session but only strings are saved (e.g. text fields), but the <input type="file"> cannot be transported to another php page. :( Here are my simple codes.

<?php
error_reporting (E_ALL ^ E_NOTICE);

function upload()
{
	move_uploaded_file($_FILES["file"]["tmp_name"],
	"try/" . $_FILES["file"]["name"]);
	echo "Stored in: " . "try/" . $_FILES["file"]["name"];
}

$form=
"<form  method='post'
enctype='multipart/form-data'>
<label for='file'>Filename:</label>
<input type='file' name='file' id='file' />
<br />
<input type='submit' name='submit' value='Submit' />
</form>";

if($_POST['submit']){
	if ((($_FILES["file"]["type"] == "image/gif")
	|| ($_FILES["file"]["type"] == "image/jpeg")
	|| ($_FILES["file"]["type"] == "image/pjpeg")
	|| ($_FILES["file"]["type"] == "image/png"))
	&& ($_FILES["file"]["size"] < 200000))
	  {
	  if ($_FILES["file"]["error"] > 0)
		{
		echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
		}
	  else
		{
		echo "Upload: " . $_FILES["file"]["name"] . "<br />";
		echo "Type: " . $_FILES["file"]["type"] . "<br />";
		echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
		echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

		if (file_exists("try/" . $_FILES["file"]["name"]))
		  {
		  echo $_FILES["file"]["name"] . " already exists. ";
		  }
		else
		  {
			echo "Do you want to continue uploading?<br />
			<a href='?yes'>Yes</a>";
		  }
		}
	  }
	else
	  {
	  echo "Invalid file\n\n".$form;
	  }
 }
 else
	echo $form;
	
if($_GET['yes'])
{
	
	move_uploaded_file($_FILES["file"]["tmp_name"],
	"try/" . $_FILES["file"]["name"]);
	echo "Stored in: " . "try/" . $_FILES["file"]["name"]";
}

?>




Is This A Good Question/Topic? 0
  • +

Replies To: How to save file to a session variable in PHP uploading form?

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3637
  • View blog
  • Posts: 5,764
  • Joined: 08-June 10

Re: How to save file to a session variable in PHP uploading form?

Posted 22 April 2013 - 07:07 PM

Hey.

In that code, your "Do you want to continue uploading?" prompt actually happens after the upload is done. Once the user clicks the yes link, you are redirecting the user to the same page again using a GET request, at which point all the data from the previous POST request is lost.

The only way that makes sense to ask the user if he wants to continue the upload before the upload is to do it in Javascript. As soon as PHP gets the file data, the upload is complete. At best you could say: [i]"You've uploaded this file. Do you want us to save it?"

If that is your plan, I suggest you move the file into a temporary storage and then move it or delete it based on the user response. And note that you can't use the initial temporary file PHP creates, as that is/can be inaccessible after the first request.


P.S.
If you do this:
move_uploaded_file($tmp, $dest);
echo "Stored in {$dest}";


You may well be lying to the user. Just calling the move_uploaded_file function does not mean that the file actually made it to the destination. There are several things that can happen that can mess that operation up. - The function returns a boolean result indicating it's success. Check that and then tell the user what actually happened. (This is true for most functions, by the way.)
if (move_uploaded_file($tmp, $dest)) {
    echo "Stored in '{$dest}'";
}
else {
    echo "Failed to move '{$tmp}' to '{$dest}'.";
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1