retrieving image from database using php - 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 !

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.

Moving Files with PHP that have been uploaded

I've gone through some of the questions, but haven't found an answer to my question so here it goes.
I've written a stereotypical script for uploading small files. The script works, and the file is uploaded to the server. However, I can't get it to upload it to move to the right subfolder.
<?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) . " MB<br>";
}
if (file_exists("/enhstudios/clients/media/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/enhstudios/clients/media/" . $_FILES["file"]["name"]);
}
?>
The error I get is:
Warning: move_uploaded_file(/enhstudios/clients/july.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /hermes/waloraweb013/b1311/moo.enhstudios/enhstudios/clients/fileuploadcode.php on line 20
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpkfxGLm' to '/enhstudios/clients/july.jpg' in /hermes/waloraweb013/b1311/moo.enhstudios/enhstudios/clients/fileuploadcode.php on line 20
Permissions for this directory are set at 777.
Where have I gone wrong? I've tried all different combos of subdirectories and still can't get it to upload to the right one.
Are you sure you don't mix the /hermes/waloraweb013/b1311/moo.enhstudios/enhstudios/clients and the /enhstudios/clients/ directories?
I think you would like to move to the longer one, but the second arguments first / makes it an absoulte path.
You can add there the full path, or you can try to remove the first /. (This relativisation works only if the document root is /hermes/waloraweb013/b1311/moo.enhstudios/)

Take file upload and go to another PHP page

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.

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.

Can't find uploaded files after PHP image upload

I'm uploading files from an iPhone app to PHP using the following code:
<?php
if ($_FILES["media"]["error"] > 0) {
echo "Error: " . $_FILES["media"]["error"] . "<br />";
} else {
echo "Upload: " . $_FILES["media"]["name"];
echo "Type: " . $_FILES["media"]["type"];
echo "Size: " . ($_FILES["media"]["size"] / 1024);
echo "Stored in: " . $_FILES["media"]["tmp_name"];
if (file_exists("uploads/" . $_FILES["media"]["name"])) {
echo $_FILES["media"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["media"]["tmp_name"],
"uploads/" . $_FILES["media"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["media"]["name"];
}
}
?>
Afterwards, I get the success message saying "uploads/2542543.jpg" - but I can't seem to find where the image is actually stored. Is the filepath relative to the php file (which is in php - and has an uploads folder) or is it absolute from the root??
EDIT: Looks like the file should have ended up in php/uploads/filename.jpg - but it doesn't appear to be making it. I don't quite understand why - anyone have any idea?
This is an absolute path from the root
/uploads/2543.jpg
This is a relative path from your PHP document
uploads/2543.jpg
So you're working with a relative path (note the missing / at the start)
This is defined in php.ini using the upload_tmp_dir directive. If that's not set, it uses the system default, which you can figure out using sys_get_temp_dir. I believe it defaults to /tmp on Linux, and C:\Windows\Temp\ on Windows.

Categories