Take file upload and go to another PHP page - php

So I have 4 pages. They are very simple.
index.php (WORKS)
<html>
<form action="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 (WORKS)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
echo 'Are you sure you want to continue saving this file';
echo 'Yes, continue
<br />
<br />
No thanks'
}
?>
no.php (WORKS)
<?php
echo 'Thanks anyway';
?>
yes.php (ERROR)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
echo 'We will now save this document:';
//Save document code
}
?>
Output of yes:
Notice: Undefined index: file in /home/public_html/test/yes.php on line 2 Invalid file
We will now save this document:?
As you can see I never save it. But I would like to save it in the yes.php page. Is it still possible to retrieve that original doc that was uploaded? Thanks in advance.

Uploaded files are only available for a single PHP instance/request cycle.
Uploaded files are stored in the temp directory. If they're still there when the script finishes executing, PHP will delete them assuming you didn't need them.
If you want to persist the file, you'll have to move it elsewhere in the same request that the file was uploaded.

You are trying to pass files from a form to a different page then intended. The post values will no longer be valid. I would suggest saving the file in the upload.php to a temporary folder and from there passing it to either the yes or no page via a $_GET[] or session variable.
On the no.php page you would take that file and use
unlink($somefile);
This will delete the file from your server.
On the yes.php page I would move or copy the file. If you copy the file, I would use unlink to remove the temp file.

You could try using move_uploaded_file() to temporarily save the file and then pass the file information using
Yes, continue
No thanks
Then retrieve the filename in yes.php using
$tmpFile = $_GET["filename"];
or remove it in no.php using
unlink($_GET["filename"]);

If the file is not too large, copy the file content in a session variable. The session will be preserved from one page to another.

Related

No temp file in MAMP whilst trying to upload - no file found [duplicate]

So I have 4 pages. They are very simple.
index.php (WORKS)
<html>
<form action="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 (WORKS)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
echo 'Are you sure you want to continue saving this file';
echo 'Yes, continue
<br />
<br />
No thanks'
}
?>
no.php (WORKS)
<?php
echo 'Thanks anyway';
?>
yes.php (ERROR)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
echo 'We will now save this document:';
//Save document code
}
?>
Output of yes:
Notice: Undefined index: file in /home/public_html/test/yes.php on line 2 Invalid file
We will now save this document:?
As you can see I never save it. But I would like to save it in the yes.php page. Is it still possible to retrieve that original doc that was uploaded? Thanks in advance.
Uploaded files are only available for a single PHP instance/request cycle.
Uploaded files are stored in the temp directory. If they're still there when the script finishes executing, PHP will delete them assuming you didn't need them.
If you want to persist the file, you'll have to move it elsewhere in the same request that the file was uploaded.
You are trying to pass files from a form to a different page then intended. The post values will no longer be valid. I would suggest saving the file in the upload.php to a temporary folder and from there passing it to either the yes or no page via a $_GET[] or session variable.
On the no.php page you would take that file and use
unlink($somefile);
This will delete the file from your server.
On the yes.php page I would move or copy the file. If you copy the file, I would use unlink to remove the temp file.
You could try using move_uploaded_file() to temporarily save the file and then pass the file information using
Yes, continue
No thanks
Then retrieve the filename in yes.php using
$tmpFile = $_GET["filename"];
or remove it in no.php using
unlink($_GET["filename"]);
If the file is not too large, copy the file content in a session variable. The session will be preserved from one page to another.

Cannot upload using php scripts

I have a script in php that is used to upload files to a server. It was working first but i dont know why its not working again. It shows no error but the file is not still uploaded to the directory that i assigned to hold all uploaded files. Here is the part that takes care of the upload:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Uploaded: " . basename($_FILES["file"]["name"]) . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
//by default the size of the file is in bytes so u need to divide by 1024 in order to bring it to KB
echo "Temporarily stored in: " . $_FILES["file"]["tmp_name"] . "<br />";
$target="/file/" . basename($_FILES["file"]["name"]) . "<br />";
}
if(move_uploaded_file($_FILES["file"]["tmp_name"], $target))
{
echo "<h2>" . "The file has been stored on the server" . "<br />" . "<h2 />";
echo "New storage location is : " . '<a href="/public/files/" >' . $target . '</a>';
?>
<html>
<body>
<div align="right"><a href="/public/files/" >Uploaded files</a></div>
<br />
</body>
</html>
<?php
}
else
{
echo "<h2>" . "Error while saving the file to the server." . "<br />" . "File wont be found in the uploaded files directory" . "<br />" . "<h2 />";
echo "The error says: " . $_FILE["file"]["error"] . " What do we do now?" ;
echo"<pre>".print_r($_FILES,true)."</pre>";
?>
<?
/file/ is a directory in the root of your server's filesystem, which almost certainly doesn't exist. move_uploaded_file() works at the filesystem level and has absolutely NO awareness of your site's URI structure. You probably want something more like:
move_uploaded_file(...,. $_SERVER['DOCUMENT_ROOT'] . '/file/');
^^^^^^^^^^^^^^^^^^^^^^^^^^^---- add this
so that the file gets moved to a /file subdir of your site's root directory.

retrieving image from database using php

<?php
$file= $_FILES["file"]["name"];//file selected in form
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>";
move_uploaded_file($_FILES["file"]["tmp_name"],
"c:/EasyPHP-12.1/www/new website/upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "c:/EasyPHP-12.1/www/new website/upload/" . $_FILES["file"]["name"];
if(($_FILES['file']['size'] >0))
{echo 'imagetrue';$con=mysql_connect('localhost','abc','YES') or die ("con");;
$db=mysql_select_db("website",$con) or die ("db");
$query=mysql_query("insert into website.picture (imagew) values ('$file')") or die('query'); //storing in db}
//echo $_FILES['file']['error'] ;
$query2= mysql_query("select * from website.picture");
$res=mysql_fetch_array($query2);
foreach($res as $row){
$str = "c:/EasyPHP-12.1/www/new website/upload/ ".$row['imagew'];
echo '<img src="'.$str.'" alt="no">' ;}
?>
i am using this script to store image and then displaying it on webpage but it is not working it only displays empty thumbnails...
I'd be very suspect about using absolute paths like c:/EasyPHP-12.1/www/new website/upload/
I'f you're then accessing the page from http://127.0.0.1/new-website (which I presume you are - or something similar) then having windows system pathnames could be a problem - do browsers even read the local filesystem like that ?
Also, it makes it very un-portable for when you move it to another server.
I'd suggest $str = 'upload/'.$row['imagew']; So it's relative to the document hosted in (I presume) 127.0.0.1/new website/image-viewer.php (or whatever you called it)
Check the return value of move_uploaded_file() - if it's false then it has failed. If that's failing try using the relative path :
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
This again presumes your script is sitting in 127.0.0.1/new-website
EDIT - NOTE ::: Is your form using enctype="multipart/form-data" ? If it's not then the images never get to the server !

PHP How to get tmp file size during Upload

I want to implement PHP uploading Progress Bar and My idea is to get the size of $_FILES['file']['tmp'] after each second.
But what is problem here, when a file is uploading there exist no file in temp directory set in php.ini but file successfully uploaded and when i try to get size of $_FILES['file']['tmp'] it show error warning.
<br />
<b>Warning</b>: filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for D:\Program Files\webserver\temp\phpDD05.tmp in <b>D:\Program Files\webserver\www\test\upload\uploading.php</b> on line <b>5</b><br />
which mean i think file not exist. How we can converge this idea to success for uploading with progress bar
You can try something like this to see if there's an error, then grab the file size if there are no errors.
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
}
?>
What does that do for you?
File is incapsulated in the POST request, so you need something outside your form that uploads the file and something that monitors periodically the file. I suggest some already made solutions like ajax upload progress bar or similar

php file uploading in background?

I am following this tutorial to upload a file using php http://www.w3schools.com/php/php_file_upload.asp
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
Using this, I have to refresh the page or redirect it, is there a way I can just upload it in the background?
Thanks
Do as all the other scripts. Create a hidden iframe, set a target on your form to that iframe and your good to.
Why not using jquery uploads plugins? You can find plenty of them and they doing a wonderful work.
Try jQuery Form Plugin
There are loads of scripts out there to help with file uploads.
Some examples:
http://pixeline.be/experiments/jqUploader/test.php
http://www.uploadify.com/demos/
Just do a google search or two!
But you will need some javascript knowledge.
Use AJAX, to read and write your requests and responses. Search on XMLHttp Requests and that may help. You can do a POST operation there.

Categories