Page 1 of 1

Phot upload script

Posted: Sat Sep 03, 2005 6:13 am
by ericburnard
hi there, my friend wrote a scitpt for me along time ago to upload photos. i have changed it slightly to do my new job. The is the script below. can anybody tell me why its giving this error -

Code: Select all

Warning: copy(users/whatever_image_i_uploaded.jpg): failed to open stream: No such file or directory in /home/madashat/public_html/eric/mpupload.php on line 26
There was an error adding the pics
The folder users is created and permissions are set to 777

Code: Select all

<?
include('http://eric.madashatters.com/header.inc')
?>

<?php

$username2 = $_GET['user'];

$username = 'madashat_eric';
$password = 'gateway2';
$database="madashat_users";
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
for($i=0;$i<10;$i++){
if($pic[$i]!=""){
	if($album[$i]==''){
		$album[$i] = 'other';
	}
	$dest1 = "users/";
		if($pic[$i]!=NULL){
		$who = $who[$i];
		$where = $where[$i];
                $comment = $comment[$i];
                $name = strtolower($_FILES["pic"]["name"][$i]);
		$result = MYSQL_QUERY("INSERT INTO `photos` ( `file` , `who` , `where` , `comment` , `user` ) VALUES ('$name','$who','$where','$comment','$username2')");
		copy($pic[$i], $dest1.$name);
		if(file_exists($dest1.$name)){
		echo '<font color=red>'.$_FILES["pic"]["name"][$i].'</font> added to <i>'.$dest1.'</i><br>';
		}else{
		echo 'There was an error adding the pics';
		}
	}
}
}
mysql_close();

echo '<form method=POST action=mpupload.php enctype="multipart/form-data">';
for($i=0;$i<10;$i++){
echo "<input type=file name=pic[$i]>Who: <input type=\"text\" name=\"who[$i]\">Where: <input type=\"text\" name=\"where[$i]\">Comment: <input type=\"text\" name=\"comment[$i]\"><br>";
}
echo "<input type=submit value=Add></form>";
?>

<?
include('http://eric.madashatters.com/footer.inc')
?>
Anythng to help would be great.

Thanks again
Eric

Posted: Sat Sep 03, 2005 6:26 am
by n00b Saibot
Plz remove username & password from your code as a security measure...

Code: Select all

mysql_connect(localhost,$username,$password);
should be

Code: Select all

mysql_connect('localhost',$username,$password);
try changing $dest1 = "users/"; to $dest1 = "./users/";

Posted: Sat Sep 03, 2005 8:17 am
by ericburnard
Thanks for reminding me i always for get.

I have tried what you suggested and got this error

Code: Select all

Warning: copy(./users/1.jpg): failed to open stream: No such file or directory in /home/madashat/public_html/eric/mpupload.php on line 26
There was an error adding the pics
i also tried $dest1 = "http://eric.madashatters.com/users/"; and got this error

Code: Select all

Warning: copy(http://eric.madashatters.com/users/1.jpg): failed to open stream: HTTP wrapper does not support writeable connections. in /home/madashat/public_html/eric/mpupload.php on line 26
There was an error adding the pics
:s

Posted: Sat Sep 03, 2005 8:50 am
by feyd
try

Code: Select all

copy($pic[$i], $_SERVER['DOCUMENT_ROOT'].'users'.DIRECTORY_SEPERATOR.$name);

Posted: Sat Sep 03, 2005 9:58 am
by ericburnard
feyd wrote:try

Code: Select all

copy($pic[$i], $_SERVER['DOCUMENT_ROOT'].'users'.DIRECTORY_SEPERATOR.$name);
Ok i tried that but i am still getting the following error.

Code: Select all

Warning: copy(/home/madashat/public_html/ericusersDIRECTORY_SEPERATOR1.jpg): failed to open stream: Permission denied in /home/madashat/public_html/eric/mpupload.php on line 26
There was an error adding the pics
Even though the permissions are correct (set to: 777)

Thanks

Eric

Posted: Sat Sep 03, 2005 11:35 am
by s.dot
I believe your error is saying that the original copy of the image should reside in users/ and it can't find it. When you upload a file it goes to a tmp location, and then should be moved to a permanent location.

Try something along the lines of

Code: Select all

move_uploaded_file($_FILES['pic']['tmp_name'][$i], "users/".$_FILES['pic']['name'][$i]);
In place of your copy line.

Also, remove the [$i] from your form input name, it should just be pic[]

then use foreach to loop over your pictures

Code: Select all

foreach($_FILES AS $k => $v)
{
   $tmp_name = $_FILES['pic']['tmp_name'][$k];
   // continue using $k in place of $i
}

Posted: Sat Sep 03, 2005 12:07 pm
by feyd
oops, DIRECTORY_SEPARATOR (misspellt it)

Posted: Sat Sep 03, 2005 12:19 pm
by ericburnard
feyd wrote:oops, DIRECTORY_SEPARATOR (misspellt it)
lol okeys ive tried that but still getting the error

Code: Select all

Warning: copy(/home/madashat/public_html/user/hair2.jpg): failed to open stream: No such file or directory in /home/madashat/public_html/eric/mpupload.php on line 26
There was an error adding the pics
still have to try scrotaye's way though

Thanks
Eric

Posted: Sat Sep 03, 2005 12:25 pm
by feyd
hang on... is this script residing in /home/madashat/public_html/ or /home/madashat/public_html/eric/ ? which one is the user folder under?

Posted: Sat Sep 03, 2005 2:49 pm
by s.dot
Definately sounds like a relative path problem. Either way, in your latest error message the directory in the error message was "user" and in your script you have $dest = "users"

dunno if u changed the directory or not, but user != users :P

also, use

Code: Select all

copy($_FILES['pic']['tmp_name'][$i], $dest/$_FILES['pic']['name'][$i]);
assuming your script resides in the correct path

Posted: Sat Sep 03, 2005 9:25 pm
by John Cartwright

Code: Select all

copy($_FILES['pic']['tmp_name'][$i], $dest.'/'.$_FILES['pic']['name'][$i]);
you had a typo scrotaye

Posted: Sat Sep 03, 2005 11:10 pm
by s.dot
yes I did.. but not the one you 'corrected' :P

I should have put a period instead of a /

his $dest ends in a /