Phot upload script

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
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Phot upload script

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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/";
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try

Code: Select all

copy($pic[$i], $_SERVER['DOCUMENT_ROOT'].'users'.DIRECTORY_SEPERATOR.$name);
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

oops, DIRECTORY_SEPARATOR (misspellt it)
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

copy($_FILES['pic']['tmp_name'][$i], $dest.'/'.$_FILES['pic']['name'][$i]);
you had a typo scrotaye
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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 /
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply