Page 1 of 1
problem with file upload script
Posted: Fri Oct 28, 2005 6:09 pm
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!";
}
?>
Re: problem with file upload script
Posted: Fri Oct 28, 2005 6:16 pm
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...???
Posted: Fri Oct 28, 2005 6:20 pm
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?
Posted: Fri Oct 28, 2005 6:32 pm
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:
Try using quotes
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

Posted: Fri Oct 28, 2005 6:42 pm
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

Posted: Fri Oct 28, 2005 8:24 pm
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?
Posted: Fri Oct 28, 2005 8:38 pm
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.
