note 19589 deleted from function.copy by alindeman
| From: | alindeman@php.net | Date: | Fri, 09 May 2003 22:15:19 +0000 |
| Subject: | note 19589 deleted from function.copy by alindeman | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-47789@lists.php.net to get a copy of this message | ||
This little bugger took a whole heck of a long time to get straightened out.
Problem: how to allow a user to upload an image.
There are a lot of good tips about how to do this in the html form, so I wont repeat that here.
The basic problem I had was not having the permissions necessary for doing a COPY or a
MOVE_UPLOADED_FILE. This would be the case if you are using a shared hosting platform.
The answer that I worked out uses an ftp open with username and password. Usually this is allowable
as you need FTP access on your account.
//Build handles for uploaded image
$imageUpFile = $_FILES['photo_file']['tmp_name'];
$imageType = $_FILES['photo_file']['type'];
$imageName = $_FILES['photo_file']['name'];
//Open the uploaded pic (temp file)
if ($thePic = fopen ($imageUpFile, "rb"))
{
//read temp file into var
$savePic = fread($thePic, filesize($imageUpFile));
//Open a new file in the correct directory using the name of the uploaded pic file
if ($fileHandle = fopen("ftp://username:password@put/directory/here/$imageName",
"w"))
{
//Put data into just opened file pointer
if (fwrite ($fileHandle, $savePic))
{
echo "file <b>$imageName</b> successfully written";
}
else
{
echo "file <b>$imageName</b> was NOT written";
}
}
fclose ($fileHandle);
fclose ($thePic);
}
else
{
// The file was not created. Inform the user.
echo ("<b> The file: $imageUpFile could not be created!</b><p>");
}
Here is a function which also helps with the above procedures:
//Function displays the contents of a directory
function display_directory( $thisDir )
{
$dh = opendir ($thisDir);
rewinddir ($dh);
echo "<h4>Directory opened</h4>";
echo "<table border=1 width=200>";
echo "<tr bgcolor=\"#3399FF\"><td>$dh</td></tr>";
while ($file = readdir ($dh))
{
echo "<tr><td>$file</td></tr>";
}
echo "</table><hr>";
closedir ($dh);
}