Hi guys.
I am having problems getting the below script to work:
if ( isset( $_POST["sendPhoto"] ) ) {
processForm();
} else {
displayForm();
}
function processForm() {
if ( isset( $_FILES["photo"] ) and $_FILES["photo"]["error"] ==
UPLOAD_ERR_OK ) {
if ( $_FILES["photo"]["type"] != "image/jpeg" ) {
echo "<p>JPEG photos only, thanks!</p>";
} elseif ( !move_uploaded_file( $_FILES["photo"]["tmp_name"],
"photos/" . basename( $_FILES["photo"]["name"] ) ) ) {
echo "<p>Sorry, there was a problem uploading that photo.</p>" .
$_FILES["photo"]["error"] ;
} else {
displayThanks();
}
} else {
switch( $_FILES["photo"]["error"] ) {
case UPLOAD_ERR_INI_SIZE:
$message = "The photo is larger than the server allows.";
break;
case UPLOAD_ERR_FORM_SIZE:
$message = "The photo is larger than the script allows.";
break;
case UPLOAD_ERR_NO_FILE:
$message = "No file was uploaded. Make sure you choose a file to
upload.";
break;
default:
$message = "Please contact your server administrator for help.";
}
echo "<p>Sorry, there was a problem uploading that photo. $message</p>";
}
}
function displayForm() {
?>
<h1>Uploading a Photo</h1>
<p>Please enter your name and choose a photo to upload, then click
Send Photo.</p>
<form action="photo_upload.php" method="post" enctype="multipart/
form-data">
<div style="width: 30em;">
<input type="hidden" name="MAX_FILE_SIZE" value="50000" />
<label for="visitorName">Your name</label>
<input type="text" name="visitorName" id="visitorName" value="" />
<label for="photo">Your photo</label>
<input type="file" name="photo" id="photo" value="" />
<div style="clear: both;">
<input type="submit" name="sendPhoto" value="Send Photo" />
</div>
</div>
</form>
<?php
}
function displayThanks() {
?>
<h1>Thank You</h1>
<p>Thanks for uploading your photo<?php if ( $_POST["visitorName"] )
echo ", " . $_POST["visitorName"] ?>!</p>
<p>Here’s your photo:</p>
<p><img src="photos/<?php echo $_FILES["photo"]["name"] ?>" alt="Photo"
/></p>
<?php
}
The above code is supposed to allow the user to browse for an image file, select it and click the send button. Basically, I keep getting the following error message when i click the 'send photo' button:
Notice: Undefined index: photo in C:\wamp\www\phpsite1\php forms\5 photo upload\photo_upload.php on line 50
In the book i am reading, after typing up the above code, it states:
Next, create a photos folder in the same folder on your Web server (the document root). This folder is
to store the uploaded photos. You’ll need to give your Web server user the ability to create files in this
folder. On Linux or Mac OS X you can do this in a Terminal window as follows:
cd /path/to/document/root
chmod 777 photos
On Windows you can use Windows Explorer to set permissions on the photos folder.
Now try running the script in your browser. You should see the form shown in Figure 9-10. Enter your
name and choose a JPEG photo to upload, then click Send Photo. You should see a thank-you message
appear along with your uploaded photo.
Basically, I just created a folder named 'photos' on my local host, on my laptop. Is this where I am going wrong? Will the above script only work on the remote host?
And if so, I have no idea what the above quote from my book actually means. especially this?:
cd /path/to/document/root
chmod 777 photos
Paul.