error when using file upload form

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
Linda
Forum Newbie
Posts: 2
Joined: Sat Feb 22, 2003 5:59 pm

error when using file upload form

Post by Linda »

Hi,

I am fairly new to PHP. I have mastered forms, but now need to add a file upload facility to one. I have searched for coding etc, and tried to apply it to my forms.

My only problem as far as I can see, is in the following line, where I tell the form where to upload the file to -
copy("$superdat", "http://www.mywebsite.com/home/mycontrol ... p/incoming /uploading/$superdat_name") or
die("Couldn't copy file.");

I can't seem to get the path/address correct, as I keep getting this message when I upload my forms and go in as a user to test them. When I click submit, this is the error that I get -

Warning: Unable to create 'http://www.mywebsite.com/home/mycontrol ... p/incoming /uploading/emaillogosmall.gif': No such file or directory in /home/mycontrolpanelloginname/public_html/upload.php on line 21
Couldn't copy file.

Of course, I have changed some of the names to put this on here. But, I think my main problem, is that in my php coding, where I have the line that tells the form where to upload the file to on my web site server, I am not putting the correct address.

The file 'uploading' has user write permissions, so that part is fine. I just can't grasp the correct way to write the actual path/address to place the file. I have tried '/public_ftp/incoming/uploading/' , I have tried 'http://www.mywebsite.com/public_ftp/incoming/uploading/' , I have tried '/home/mycontroplanelusername/public_ftp/incoming/uploading/' , and I have also tried placing the 'uploading' file just straight on my hositing directory and also in my 'public_html' file. Nothing seems to work. I am on a shared Web Hosting service, does this make a difference?

How do I correctly write the path/address on a shared Unix server?
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

You need the complete file path to your folder. URLs will not work, nor will partials such as "/home/mycontroplanelusername/public_ftp/incoming/uploading/". You need to have the full path from the root (ex: /usr/home/your_profile_name/www/htdocs/folder_name - this is what it is on my server)
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

Post by decoy1 »

Change path to this and see what happens

Code: Select all

$DOCUMENT_ROOT/home/mycontroplanelusername/public_ftp/incoming/uploading/
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post by bionicdonkey »

this is a script i wrote for uploading images:

Code: Select all

<?
if(isset($invoke) == 'Upload Image') {

	$username = $authdata['username'];
	$filename = $HTTP_POST_FILES['userfile']['name'];
	if(substr($HTTP_POST_FILES['userfile']['type'], 0, 5) != 'image') { // Check if image
		die("Upload Aborted! Uploaded file not an image.");
	}
	if(file_exists("../images/".$filename)) { // If file already exists
		for($i = 1; $i <= 1000; $i++) {
			$tmp = explode(".", $filename);
			$file = $tmp[0];
			$ext = $tmp[1];
			if(!file_exists("../images/".$file.$i.$ext)) {
				$filename = $file.$i.".".$ext; // rename new file
				break;
			}
		}
	}	
	mysql_query("INSERT INTO images VALUES ('$filename', '$imagename', '$username')");
	
	if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
		move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'], "../images/". $filename); // Move to new location
	} else {
		echo "Possible file upload attack. Filename: " . $HTTP_POST_FILES['userfile']['name'];
	}
	echo "Data submited";
	
}

?>
Linda
Forum Newbie
Posts: 2
Joined: Sat Feb 22, 2003 5:59 pm

Post by Linda »

Decoy1 Thank you so very very much! I am a very happy girl!

Your method worked! I only needed to make one change to it, that was - I cut out the middle stuff and just put - $DOCUMENT_ROOT/uploading/
and it worked at last. It is good that I had to cut out the middle stuff including my controlpanelloginname as I would hate for somebody to hack in and find out what my username is.

Bionicdonkey, I still would like to find out whether your code involves sessions and users with usernames, and if so whether I can cut that line out, as it is always great to know of other codes that work, so that they can be combined to suit my changing needs.

Thank you both very much, I am so releived to find the answer at last. :lol:
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

Post by decoy1 »

Glad it worked :)
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post by bionicdonkey »

it does involve sessions and users. authdata is a session variable which contains data about the user that is logged in. $username is required in the db table. it can be safely removed.
Post Reply