Hello,

I am wanting to take an uploaded picture from one page, put it into a session and carry it to another page. Once I get to that page and the client accepts it, then store it in the database. I'm starting to get confused on how this would work because of it being stored into a temporary folder.

Can anyone give me insight on how this would work and the best way to do it?

Thanks!
~Amy

Recommended Answers

All 22 Replies

Is it possible to save the image into a temporary folder and save the path in a session? Or when I go to the next page does it erase the image? Am I just going to have to save it in the database and then save that path in a session?

Any help is appreciated!
~Amy

Hi Amy,
I think its better to store it in a temporary folder.. not the php temp folder, but the one you create....
1) Instead of saving it directly to the database, first store it in some location, say temp_upload_folder (custom folder you created)..
2)Store the files location & name in the session & go to users approval page
3) get approval from user
4) If user rejects, delete the file from the location and move back to the path..
5) If user accepts, store the image file in database

You try for this algo.. I'll also try for some code for this...

If I create a custom folder for this, where would be a good place to put it? Should it be in the same directory as the php temp folder or does it matter?

Okay, so it stays in this new folder I create and I just store the name of where it is located in the database if it is accepted.

Yes, if you can help me find an example code that would be wonderful so I can see how it is done and then write my own thing :)

~Amy

Okay, I think I am closer! How does this look for the first part?

<?php
session_start();

include ("databaseinfo.inc");

mysql_connect ($host, $user, $password, $database) or die ("No Connection");

$allowed=array('image/pjpeg', 'image/jpeg', 'image/jpg', 'image/png', 'image/gif');

if ((in_array($_FILES['pic1']['type'], $allowed)) && ($_FILES['pic1']['size']<25600))
{
move_uploaded_file($_FILES['pic1']['tmp_name'], ".../uploads/{$_FILES['pic1']['name']}");
$_SESSION['name']=$_FILES['pic1']['name'];
$_SESSION['tmp_name']=".../uploads";
}
else
{
$errors[]='The first file you submitted is not working.  The file must be under 25 kb and have a .jpg, .jpeg, .gif, or .png extension only!';
}
//I have other things to validate and then I check my errors...   
if (empty($errors))
{
header 
("Location:http://www.nextpage.php");
exit();
}

if (!empty($errors) && is_array($errors))
{
echo '<h1>Error!</h1>
The following error(s) occured:<br/ >';
foreach ($errors as $msg)
{
echo " - $msg<br />\n";
}
}
?>

Hi..
This first part looks good...
That means, the file is now getting uploaded on the uploads directory..

Now the next part is to get this file validated by the user

<?php
//If the user does not like the image
if($_POST['dont_save']=="true")
{
    unlink(".../uploads/$_SESSION['name']");
}
else
{
    $myFile = $_SESSION['name'];
    $fh = fopen($myFile, 'r');
    $theData = fread($fh, filesize($myFile));
    fclose($fh);
    
    //Now store the $theData in the database..
}

Hope it works out for you....

Okay, that didn't work. It showed an error:
No such file or directory
even though I know it does exist. Not sure what to do now. Any ideas?
~Amy

Seems you have kept your file inside some folder..
something like..
-uploads
-some_directory
-your_php_file

Just change the path name from ".../uploads" to "..uploads"

Hey.. wait a minute... may be i dint mentioned.. wenever you want to upload a file, then you have to give the complete absolute path..
i.e. c:\xampp\htdocs\mysite\uploads\<?php $filename ?>

Even http://mysite.com/uploads will not work..

You have to give just the complete absolute name...

Try now!!!

I did what you said and these are the 2 error messages:

Warning: move_uploaded_file [file] [function.move-uploaded-file]: failed to open stream: Permission denied on line 15

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpsIucw9' to [file] on line 15

I'm unsure what to do because the people who own my server told me this is where I should upload files. Any other reason it wouldn't work?

Okay, I may have gotten this figured out by changing the chmod. However, now that I have that up, I get this red X. What does that mean in regards to my code? Here's what I have on the page I'm pulling that info:

<?php
session_start();

include ("databaseinfo.inc");

mysql_connect ($host, $user, $password, $database)
or die ("No Connection");

$tempFile=$_SESSION['tmp_name'];
echo "<img src='$tempfile' alt='Oops, not showing'>";
?>

So I get a red X and "Oops, not showing".

Hmmmmmm....
???
~Amy

Hi..
Whenever there is a permissions error, then you have to use chmod thing.. so u got that right...

And as far as the above thing is concerned, why are you using the temp_name...
The temporary name is given to the file that is created while uploading the file and it is automatically deleted... And moreover it is not stored in the other folder...

Use the $_FILES attribute which is stored in the session.. i.e. $_SESSION and dont forget to use the relative path src="../uploads/$filename" in the img tag..

I see what you mean, but the same thing happens when I change it to this:

<?php
session_start();

include ("databaseinfo.inc");

mysql_connect ($host, $user, $password, $database)
or die ("No Connection");

$tempFile=$_SESSION['name'];
echo "<img src='.../uploads/$tempfile' alt='Oops, not showing'>";
?>

Do you know of anything else that would cause this?
Thanks!
~Amy

I know these files are going in...they just aren't being pulled out correctly, but I can't figure out why

Hi..
It would be great if you could post me the whole code..
Including where the sessions are being set... Just to review..
Because from the above code, seems no problem here...

This is from the first page:

<?php
session_start();

include ("databaseinfo.inc");

mysql_connect ($host, $user, $password, $database) or die 
("No Connection");

$allowed=array('image/pjpeg', 'image/jpeg', 'image/jpg', 
'image/png', 'image/gif');

if ((in_array($_FILES['pic1']['type'], $allowed)) && 
($_FILES['pic1']['size']<25600))
{
move_uploaded_file($_FILES["pic1"]["tmp_name"],  ".../uploads/" . $_FILES["pic1"]["name"]);
$_SESSION['name']=$_FILES['pic1']['name'];
$_SESSION['tmp_name']="/uploads/" .$_FILES["pic1"]["name"];
}
else
{
$errors[]='The first file you submitted is not working.  The file 
must be under 25 kb and have a .jpg, .jpeg, .gif, or .png 
extension only!';
}
if (empty($errors))
{
header 
("Location:http://www.thispage.com");
exit();
}

if (!empty($errors) && is_array($errors))
{
echo '<h1>Error!</h1>
The following error(s) occured:<br>';
foreach ($errors as $msg)
{
echo " - $msg<br />\n";
}
}
?>

This is from the last page

<?php
session_start();

include ("databaseinfo.inc");

mysql_connect ($host, $user, $password, $database)
or die ("No Connection");

$tempFile=$_SESSION['name'];
echo "<img src='.../uploads/$tempfile' alt='Oops, not showing'>";
?>

Hmmmm....

Oh, sorry-the last part is...

echo "img src='.../uploads/$tempFile' alt='Oops, not showing'>";

The last part should be..

<?php
echo "img src='.../uploads/$tempFile' alt='Oops, not showing'>"; 
?>

Im sorry...

It should be

<?php
echo "<img src='.../uploads/$tempFile' alt='Oops, not showing'>"; 
?>

Yes, but that isn't working either. Not sure what's going on.

Hi...
In that echo part, use,

<?php
echo "<img src='../uploads/$tempFile' alt='Oops, not showing'>"; 
?>

Okay, this is what I posted.

<?php
session_start();

include ("databaseinfo.inc");

mysql_connect ($host, $user, $password, $database)
or die ("No Connection");

$tempFile=$_SESSION['name'];
echo "<img src='.../uploads/$tempFile' alt='Oops, not showing'>";
?>

It didn't work though :(
Hmmmmm....???

I think you dint read my post carefully...

I posted

<?php
echo "<img src='../uploads/$tempFile' alt='Oops, not showing'>"; 
?>

I have put only two dots...
Try this...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.