Hello all, I asked this question before but it's been awhile ago and I can't find where I asked it. But anywho, it's never been solved.

On one page I want a person to upload a picture and my php code checks it and puts it in a directory on my server. Then on the next page they can view the picture. If they accept it, the whereabouts of the picture is saved in the database. If not, it is erased from the directory.

I've tried writing this code, but nothing seems to work. I am able to put it into this folder, but when I go to the next page, there is a Red X. It seems I can't pull it out correctly. Does anyone have any experience with this?

Thanks!
~Amy

i have done this before. post your code and I will help you make the necessary changes.

Okay...here's the html form part:

<html>
<FONT style="FONT-SIZE: 12pt" face=Arial>
Upload Picture:
</FONT>
<TABLE>
<TBODY>
<FORM onreset="return confirm('Do you really want to reset the form?')" action=previewpic.php method=post encType=multipart/form-data>
<TR>
<TD>
<FONT style="FONT-SIZE: 11pt" face=Arial>
Choose Your Picture:
</FONT>
</TD>
<TR>
<TD>
<INPUT type=hidden value=200000 name=MAX_FILE_SIZE>
<INPUT tabIndex=1 type=file name=pic1>
</TD>
</TR>
</TBODY>
</TABLE>
<INPUT tabIndex=7 type=reset value=reset name=reset>
<TD>
<INPUT tabIndex=8 type=submit value=Submit>
</TD>
</FORM>
</html>

Then previewpic.php looks like this:

<?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.previewpage.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";
}
}
?>

Then on the next page the following code is there to pull the pic out of that directory:

<?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'>";

?>

But when I go to that page, there is a Red X with "Oops, not showing".

Any ideas?

make sure the path to the image is right. make sure the image is there.

Well, I did. I made sure the image goes in...but it never comes out. That's why I was wondering if something is wrong about my code. Now everytime I get to that page, something from my server pops up asking for username and password. Is that a permissions problem?

Okay...here's the html form part:

move_uploaded_file($_FILES["pic1"]["tmp_name"], "../uploads/" . $_FILES["pic1"]["name"]);

hi there,
i think you need to make sure image is properly uploaded in to the folder. For this you can use

$result = move_uploaded_file($TempFileName, $FilePath);

					if (!$result) 
					{
						echo "Error in uploading file"; 
						exit;
					}
					chmod($FilePath,0777);

chmod change permission to that folder. Also you need to manually change permission using your FTP. Simply right click on that folder in FTP and choose CHMOD option change it to WRITE ALL.
If problem persist please reply.
Hope this will help you.
Thanks & Regards
Dilip Kumar Vishwakarma

Thank you for you answer. I went to that exact directory I save the files to and it shows it in there, so that's how I knew the picture saved.

I don't have CHMOD option but I went into my server and where it has permissions, read, write, and execute is all checked for owner, group, and others, which seems a little insecure to me. It also has a category for Ownership-User and Group. Are you saying I should try to change it FTP and not manually?


Thank you,
~Amy

Okay, I tried it through FTP, same ordeal. I don't know why I thought it would be different. The problem isn't the picture going in, it's getting it out :( Any ideas what would cause something from the serve to pop up asking for username and password?
Thanks,
~Amy

Am I supposed to include some sort of FTP information?

try uploading it to the same directory the script is in. then change the path of the image as well and see if it shows up.

Okay, here is what I'm thinking...when I upload the picture it goes into the correct directory. But when I try to pull it out, it won't let me. So I added this to my php code:

chmod ($_SESSION['tmp_name'], 0777);

This changed the permissions, but still nothing changed-I still get the same pop up box from the server when I try to pull it from the temp folder. Do you think I should be changing chown too?
Any feedback will be appreciated!
Thanks,
~Amy

I realize the ownership (user and group) are both www-data. I have been googling this trying to figure out why it's not working, if there is something I can do, but I'm coming up short. Anyone know anything about www-data?
Thanks,
~Amy

Okay, I think I got it figured out. The directory I was uploading to was protected so I got that fixed. However, the problem I have now is that my code only accepts .gif pictures. Why would that be?

Again, here's that part it:

<?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.previewpage.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";
}
}
?>
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.