Image Upload

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
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Image Upload

Post by AliasBDI »

I have an upload script that I'm trying to execute and I have noticed that the copy() function does not work. I know that there are some things that don't seem to work correctly on a WINDOWS machine and didn't know if this is one of them. Below is my code and my error below that. What do you think?

Code: Select all

//user defined variables
$uploadPath = "/articles/graphics/db/"; //Absolute path to where images are uploaded. No trailing slash
$uploadSizeLim = "no"; //Do you want uploadSizeLitAmt limit, yes or no
$uploadSizeLitAmt = "2500000"; //What do you want uploadSizeLitAmt limited to be if there is one
$uploadNum = 1;  //Number of uploads to occur


if ($_REQUEST['submitted']){ // Begin processing portion of script


//all image types to upload
$cert1 = "image/pjpeg"; //Jpeg type 1
$cert2 = "image/jpeg"; //Jpeg type 2
$cert3 = "image/gif"; //Gif type
$cert4 = "image/ief"; //Ief type
$cert5 = "image/png"; //Png type
$cert6 = "image/tiff"; //Tiff type
$cert7 = "image/bmp"; //Bmp Type
$cert8 = "image/vnd.wap.wbmp"; //Wbmp type
$cert9 = "image/x-cmu-raster"; //Ras type
$cert10 = "image/x-x-portable-anymap"; //Pnm type
$cert11 = "image/x-portable-bitmap"; //Pbm type
$cert12 = "image/x-portable-graymap"; //Pgm type
$cert13 = "image/x-portable-pixmap"; //Ppm type
$cert14 = "image/x-rgb"; //Rgb type
$cert15 = "image/x-xbitmap"; //Xbm type
$cert16 = "image/x-xpixmap"; //Xpm type
$cert17 = "image/x-xwindowdump"; //Xwd type

$log = "";


for ($i=0; $i<$uploadNum; $i++) {

	//checks if file exists
	if ($img_name[$i] == "") {
		$log .= "No file selected for upload $i<br>";
	}

	if ($img_name[$i] != "") {
		//checks if file exists
		if (file_exists("$uploadPath/$img_name[$i]")) {
			$log .= "File $i already existed<br>";
		} else {

			//checks if files to big
			if (($uploadSizeLim == "yes") && ($img_uploadSizeLitAmt[$i] > $uploadSizeLitAmt)) {
				$log .= "File $i was too big<br>";
			} else {


				//Checks if file is an image
				if (($img_type[$i] == $cert1) or ($img_type[$i] == $cert2) or ($img_type[$i] == $cert3) or ($img_type[$i] == $cert4) or ($img_type[$i] == $cert5) or ($img_type[$i] == $cert6) or ($img_type[$i] == $cert7) or ($img_type[$i] == $cert8) or ($img_type[$i] == $cert9) or ($img_type[$i] == $cert10) or ($img_type[$i] == $cert11) or ($img_type[$i] == $cert12) or ($img_type[$i] == $cert13) or ($img_type[$i] == $cert14) or ($img_type[$i] == $cert15) or ($img_type[$i] == $cert16) or ($img_type[$i] == $cert17)) {
					@copy($img[$i], "$uploadPath/$img_name[$i]") or $log .= "Couldn't copy image 1 to server<br>";
					if (file_exists("$uploadPath/$img_name[$i]")) {
						$log .= "File $i was uploaded<br>";
					}
					} else {
						$log .= "File $i is not an image<br>";
					}
				}
			}
		}


	}
Error:
Couldn't copy image 1 to server
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

remove the "@" to see the error it may be kicking out.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Here is the error:
Warning: copy(/articles/graphics/db/_th.jpg): failed to open stream: No such file or directory in c:\domains\t411.com\wwwroot\test.php on line 56

Log:
Couldn't copy image 1 to server
Line 56 reads:

Code: Select all

copy($img[$i], "$uploadPath/$img_name[$i]") or $log .= "Couldn't copy image 1 to server<br>";
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Look at the filename you're trying to copy. Is it really _th.jpg?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Yes. That is the name of the file.
User avatar
SomeOne
Forum Commoner
Posts: 27
Joined: Tue Jul 25, 2006 2:06 am

Post by SomeOne »

i think maybe there is problem with this

Code: Select all

"$uploadPath/$img_name[$i]"
check the PATH if it is correct...
i had the same problem....and on the end it was not correct path....
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

The path is correct. Here is a file that manually uploaded:
http://t411.com/articles/graphics/db/im ... ipture.jpg
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

If this is in Windows, can you really reference an absolute path starting with '/' - don't you need a drive letter?
I'm not meaning to second guess you, but I find it hard to believe that /articles/graphics/db/_th.jpg is the 100% correct path, when PHP is telling you that path doesn't exist.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Sorry, pickle. You were right. I thought that the way I sourced my path was permitted. I removed the slashes and made it relative. It did indeed upload. Thanks for your help.
Post Reply