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

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”][“t…
“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’ />

<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”] < 20))
{
if ($_FILES[“file”][“error”] > 0)
{
echo “Return Code: ” . $_FILES[“file”][“error”] . “
“;
}
else
{
echo “Upload: ” . $_FILES[“file”][“name”] . “
“;
echo “Type: ” . $_FILES[“file”][“type”] . “
“;
echo “Size: ” . ($_FILES[“file”][“size”] / 1024) . ” Kb
“;
echo “Temp file: ” . $_FILES[“file”][“tmp_name”] . “
“;

if (file_exists(“try/” . $_FILES[“file”][“name”]))
{
echo $_FILES[“file”][“name”] . ” already exists. “;
}
else
{
echo “Do you want to continue uploading?

<a href=’?yes’>Yes</a>”;
}
}
}
else
{
echo “Invalid filenn”.$form;
}
}
else
echo $form;

if($_GET[‘yes’])
{

move_uploaded_file($_FILES[“file”][“t…
“try/” . $_FILES[“file”][“name”]);
echo “Stored in: ” . “try/” . $_FILES[“file”][“name”]”;
}

?>

✅ Answers

? Best Answer

  • See the guide here: http://php.net/manual/en/session.upload-…
  • Leave a Comment