problem with file upload script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

problem with file upload script

Post by kristie380 »

Can someone tell me what I'm doing wrong? I'm trying to get my code to process a file upload but it is not working. Here is my php script:

Code: Select all

<?php
	
if(is_uploaded_file($_FILES['photo']['tmp_name'])) {
move_uploaded_file($_FILES['photo']['tmp_name'], "/photos/{$_FILES['photo'] ['name']}");

echo "<font color=\"#006633\" size=\"5\"><p>Thank you, <b>$_POST[firstname]</b>, for submitting your photo!</p></font>";
echo "<p><font color=\"#006633\" size=\"3\"><b>Your e-mail address is:</b> $_POST[email].</p>";
echo "<p><b>You Graduated in:</b> $_POST[gradyear]</p>";
echo "<p><b>Your photo:</b><br>$_POST[photo]</p>";
echo "<p><b>Your photo description was:</b>$_POST[description]</p>";
echo "<p>If your username and password matches our database, we will post your photo with 48-72 hours!</font></p>";

} else {
    echo "There was an error uploading the file, please try again!";
} 

?>
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: problem with file upload script

Post by alex.barylski »

kristie380 wrote:Can someone tell me what I'm doing wrong? I'm trying to get my code to process a file upload but it is not working. Here is my php script:

Code: Select all

<?php
	
if(is_uploaded_file($_FILES['photo']['tmp_name'])) {
move_uploaded_file($_FILES['photo']['tmp_name'], "/photos/{$_FILES['photo'] ['name']}");

echo "<font color="#006633" size="5"><p>Thank you, <b>$_POST[firstname]</b>, for submitting your photo!</p></font>";
echo "<p><font color="#006633" size="3"><b>Your e-mail address is:</b> $_POST[email].</p>";
echo "<p><b>You Graduated in:</b> $_POST[gradyear]</p>";
echo "<p><b>Your photo:</b><br>$_POST[photo]</p>";
echo "<p><b>Your photo description was:</b>$_POST[description]</p>";
echo "<p>If your username and password matches our database, we will post your photo with 48-72 hours!</font></p>";

} else {
    echo "There was an error uploading the file, please try again!";
} 

?>
Just an observation and I may be completely off...I don't use string interpolation...but...

Are super globals (ie: $_POST, etc) allowed to be interpolated inside strings???

I notice at the beginning of your code you use {$_POST} to embed variables in strings...which might work, but in other places you don't...???
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post by kristie380 »

Ok I changed the code to the following and I am still not getting anything...

Code: Select all

<?php
	
if(is_uploaded_file($_FILES['photo']['tmp_name'])) {
move_uploaded_file($_FILES['photo']['tmp_name'], "/photos/");

echo "<font color=\"#006633\" size=\"5\"><p>Thank you, <b>$_POST[firstname]</b>, for submitting your photo!</p></font>";
echo "<p><font color=\"#006633\" size=\"3\"><b>Your e-mail address is:</b> $_POST[email].</p>";
echo "<p><b>You Graduated in:</b> $_POST[gradyear]</p>";
echo "<p><b>Your photo:</b><br>$_POST[photo]</p>";
echo "<p><b>Your photo description was:</b>$_POST[description]</p>";
echo "<p>If your username and password matches our database, we will post your photo with 48-72 hours!</font></p>";

} else {
    echo "There was an error uploading the file, please try again!";
} 

?>
Any other suggestions?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

kristie380 wrote:Ok I changed the code to the following and I am still not getting anything...

Code: Select all

<?php
	
if(is_uploaded_file($_FILES['photo']['tmp_name'])) {
move_uploaded_file($_FILES['photo']['tmp_name'], "/photos/");

echo "<font color="#006633" size="5"><p>Thank you, <b>$_POST[firstname]</b>, for submitting your photo!</p></font>";
echo "<p><font color="#006633" size="3"><b>Your e-mail address is:</b> $_POST[email].</p>";
echo "<p><b>You Graduated in:</b> $_POST[gradyear]</p>";
echo "<p><b>Your photo:</b><br>$_POST[photo]</p>";
echo "<p><b>Your photo description was:</b>$_POST[description]</p>";
echo "<p>If your username and password matches our database, we will post your photo with 48-72 hours!</font></p>";

} else {
    echo "There was an error uploading the file, please try again!";
} 

?>
Any other suggestions?
I'm pretty sure super globals cannot be interpolated normally as in:

Code: Select all

echo "This is a string $_POST['sdff']";
For obvious reasons...you'd be potentially terminating your strings without meaning to do so...

Perhaps if you use the {$_POST} approach...you might be able to interpolate variables...I don't know...never tried it!!!

Second note...

Not sure if it's legal to do the following:

Code: Select all

$_POST[firstname]
Try using quotes

Code: Select all

$_POST['firstname']
And instead of using interpolation...

use this approach:

Code: Select all

echo 'This is a string '.$_POST['firstname'].' which displays first name';
You can of course use double quotes if you want, but I personally ALMOST always use singles...

Cheers :)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Just a note Kristie380

http://codewalkers.com/tutorials/45/4.html

Turns out you can embed arrays inside double quoted strings using complex syntax...I thought I had seen that usage before :)

Read the above link if your interested :)
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post by kristie380 »

Ok I am still having problems...Here is my latest attempt:

Code: Select all

<?php
	
$target_path = "/photos/";

$target_path = $target_path . basename($_FILES['photo']['name']); 

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target_path)) {
    echo "The file ". basename($_FILES['photo']['name']). " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
} 

?>
Any suggestions?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

/photo doesn't exist, likely... that'd be a directory in the root of the path system... I'm going to guess that you are thinking it'd be the directory inside your web root, in which case using $_SERVER['DOCUMENT_ROOT'] would be the way to go. :)
Post Reply