Page 1 of 1
Debugging an upload problem
Posted: Wed Aug 06, 2003 1:58 pm
by banpro
Okay, I'm trying to allow users to upload their pictures for a personal profile page. Here's the part of the script that I have which (according to the learning php book I have should work) is supposed to upload the image to a tmp directory, then copy it to the user's directory ($user) which has just been created. However, it doesn't seem the file is uploading at all? Any help or suggestions on why this isn't working would be appreciated.
Code: Select all
mkdir("$user");
touch("$user/index.php");
$file_dir="home/user/public_html/community/tmp";
if($fupload_type == "image/gif")
{
copy($fupload, "$user/$fupload_name");
}
Thanks in advance,
Scott
Posted: Wed Aug 06, 2003 2:25 pm
by oldtimer
Here is what I use for uploading files.
Code: Select all
<?php
$limit_ext = "yes"; //limit the file type uploads
$ext_count = "4"; //number of types allowed
$extensions = array(".twa",".zip",".txt",".zrx"); //allowed file extensions
// name of the directory that your uploads will be kept in
$dirname = "files/";
// the path that the uploads directory will be placed in
// the full path of something like: http://www.codephobia.com/test/
define(PATH, "/var/www/myspace/");
// gets just the file name and extension from the file input
// in the form
$filename = $myfile_name;
// check if they actually placed something in the file input
if ($filename != "")
{
// check if the directory exists
// if it doesnt exist, make the directory
if (!$dir = @opendir(PATH.$dirname))
// make the directory (sets chmod to 700)
mkdir(PATH.$dirname, 0777) or die("<b>Error:</b> could not make directory.");
//--------
$words = strtolower($filename);
$ext = strrchr($words,'.');
if (($limit_ext == "yes") && (!in_array($ext,$extensions))) {
echo "File is wrong type";
exit;
}
//-------------------
if(file_exists(PATH.$dirname.$filename)) {
echo "File already exists";
exit;
}
// copy the file from the temporary upload position to where you want it
copy($myfile, PATH.$dirname."/".$filename) or die("<b>Error:</b> could not copy file.");
// delete the temporary uploaded file
unlink($myfile) or die("<b>Error:</b> could not delete uploaded file.");
// tell them it worked
echo "File uploaded successfully.<BR>";
}
else
{
// tell them to browse for a file
echo "You must select a file to upload.";
}
?>
Note the extensions. You can put .jpg, .gif or whatever you want. They are limited by 2 megs by system default.
Posted: Wed Aug 06, 2003 6:57 pm
by banpro
Thanks!!! That gave me what I needed to figure out why my script wasn't working as expected.
Like this forum a lot,
Scott