Jump to content

PHP Help..


Recommended Posts

I'm in a PHP class right now, and as sort of an extra credit thing, I'm doing an image uploader as part of my project..

 

 

 

 

 

 

 

Running into some problems, though..

 

 

 

 

 

 

 

Just to learn things, I took the code off of php.net's file uploading manual:

 

 

 




















   Send this file: 











 

 

 

<?php



// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead



// of $_FILES.







$uploaddir = '/var/www/uploads/';



$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);







echo '';



if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {



  echo "File is valid, and was successfully uploaded.\n";



} else {



  echo "Possible file upload attack!\n";



}







echo 'Here is some more debugging info:';



print_r($_FILES);







print "";







?>

 

 

 

 

 

 

 

I've changed __URL__ to the url I'm using, and the dir to the dir I'm using..

 

 

 

 

 

 

 

When I try it, I get "Warning: move_uploaded_file(/uploads/): failed to open stream: Permission denied in blablabla"

 

 

 

 

 

 

 

I set the permissions on the dir to which its uploading to allow everything (777 ( I don't know what I'm talking about here. )), but it still doesn't work.

 

 

 

 

 

 

 

 

 

 

 

Any help would be appreciated.

IRKAa.jpg
Link to comment
Share on other sites













woo-yay.gif







<?php







if ($posted=="")



{



?>







To upload an image, use this thingy:















<?if ($password!='') print 'Password: '; ?>















<?



}







if ($posted!="")



{



echo "

";







if ($pass!=$password) die ('That's no password!');







if(!isset($_FILES)) $_FILES = $HTTP_POST_FILES;



if(!isset($_SERVER)) $_SERVER = $HTTP_SERVER_VARS;







// What was the name of the image?



$file_name = $_FILES["immagine"]["name"];







//Let's see the extension



$file_name=strtr($file_name,"'\"\\"," ");



$n = strrpos($file_name, ".");



if ($n === false) $n = strlen($file_name);







$file_base = (substr($file_name, 0, $n));



$file_ext = substr($file_name, $n);







if ($n==0)



{



die("Please select a file to upload");



}







if ( strtolower($file_ext)=='.gif' || strtolower($file_ext)=='.jpg' || strtolower($file_ext)=='.png')



{



}



else



{



die ("Only gif, jpg or png files are allowed");



}







// Let's see what would be the finale name, once put in the image dir.



$imgPath = $_SERVER["PATH_TRANSLATED"];



if (strlen($imgPath) <= 0) $imgPath = $_SERVER["SCRIPT_FILENAME"];



$imgPath = dirname($imgPath) . $pict_dir."/";







// Check if file uploaded



if (trim($_FILES["immagine"]["name"]) == "") die("

Please select the file to upload!







Back
");







// put the file in place



if (is_uploaded_file($_FILES["immagine"]["tmp_name"]))



{



// initialize counter



$n = 0;



$imgFile = $imgPath . $file_base . $file_ext;







// Check if the filename is free



while(file_exists(strtolower($imgFile)))



{



// the file exists, create the filename_00 variant



$file_name = $file_base . "_" . sprintf("%02d", $n) . $file_ext;



$imgFile = $imgPath .$file_name;







// Increase the counter, in case we need _01



$n++;



}







$imgFile=strtolower($imgFile);







// Now move the file to its place with its name



move_uploaded_file($_FILES["immagine"]["tmp_name"], $imgFile)



or die(" Eror moving the uploaded file, check permissions and existance of the directory");







}



else die("Errors in uploading the file " . $file_name);







$file_name=strtolower($file_name);



$imgcount++;







if ($n==0)



{



echo "The upload of your image was successful

";



}



else



{



echo "PLEASE NOTE:

";



echo "The upload of your image was successful, but as another file exists with that name it has been renamed to " .$file_name. "

";



}







?>











To link to your image use this code:







<img src="<? echo $http_dir.$file_name;?>">







This is a preview of your image:











<? echo $http_dir.$file_name?>







<? } ?>















~Dan64Au

Since 27 Aug 2002

Link to comment
Share on other sites

Ok, I got it working on one host, but not on another server, and it's the exact same code, so, I guess the server isn't configured properly?

 

 

 

 

 

 

 

The CHMOD (I don't know exactly how it works, just copying from instructions for a gallery script) on some hosts, for example Lycos / Tripod, may need to be set to a 4 digit code instead (which means it'll use some weird kind of extension to the regular CHMOD, again, don't ask me)

 

 

 

 

 

 

 

Link to the instruction I'm talking about: http://yapig.sourceforge.net/doc/en/install.html

Link to comment
Share on other sites

Just chmod every file your using.

 

 

 

 

 

 

 

On certain host e.g. free host they do not allow filing system which is what your trying to do, if the host allows mysql you can upload the pictures to your mysql in binary which is not hard and many tutorials on it.

 

 

 

 

 

 

 

If you just give us an error report with your first code I could probaly debug it.

Link to comment
Share on other sites

I'm thinking the server I'm using just has some error with the configuration. I've tried every possible permission configuration to no yeild, I've tried an extremely simple code and it doesn't work. I've tried the code from php.net verbatum (meaning all I changed was the file references), and nothing has done any good.

IRKAa.jpg
Link to comment
Share on other sites

I'm thinking the server I'm using just has some error with the configuration. I've tried every possible permission configuration to no yeild, I've tried an extremely simple code and it doesn't work. I've tried the code from php.net verbatum (meaning all I changed was the file references), and nothing has done any good.

 

 

 

 

 

 

 

Are you sure the host actually allows file uploads?

 

 

 

 

 

 

 

~edit: oh, and check the maximum file upload size. Somehow one of my hosts only allowed uploads up to 200kb. Which meant I had trouble getting my gallery online :)

Link to comment
Share on other sites

Check if everything is chmoding correctly, on one of my old servers I uploaded all the php to find out my (free) host didnt allow chmoding :/ this is when i realized im better off just paying for hosting.

Link to comment
Share on other sites

Oh, sorry, I should have been more clear on my current problem..

 

 

 

 

 

 

 

File uploads I've gotten to work. What I can't get to work is seemingly any of the image functions. (imagecreate, imagejpeg, etc..)

IRKAa.jpg
Link to comment
Share on other sites

Oh, sorry, I should have been more clear on my current problem..

 

 

 

 

 

 

 

File uploads I've gotten to work. What I can't get to work is seemingly any of the image functions. (imagecreate, imagejpeg, etc..)

 

 

 

 

 

 

 

Are you sure the appropriate libraries are installed on your host? (ImageMagic or whatever you're using)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.