Php help

k, first off, I’m a bit of a php newbie, so be nice :stuck_out_tongue:

I’ve been messing around in php recently with a couple of image scripts, but I’ve run into a problem. How the script (should) work is the user uploads an image to the script and the script dumps it into a folder e.g. Problem is, it always returns the “No file selected for upload” error.

After some reading around online to try and find what might be causing the problem, I found out that some hosters don’t allow users to upload files through php. Now my question is, is Dreamhost one of these hosters? If not, does anyone know how to solve this problem? If you’ve read this far, thanks for reading even if you can’t help :slight_smile: Heres my code btw:

<? //user defined variables $abpath = "home/user/"; //Absolute path to where images are uploaded. No trailing slash $sizelim = "yes"; //Do you want size limit, yes or no $size = "1048576"; //What do you want size limited to be if there is one //all image types to upload $cert1 = "image/pjpeg"; //Jpeg type 1 $cert2 = "image/jpeg"; //Jpeg type 2 $cert3 = "image/gif"; //Gif type $log = ""; $random1 = rand(1, 99999999); //begin upload 1 //checks if file exists if ($img1_name == "") { $log .= "No file selected for upload "; } if ($img1_name != "") { //checks if file exists if (file_exists("$abpath/$img1_name")) { $log .= "Image already exists on the server "; } else { //checks if files to big if ($sizelim == "yes") { if ($img1_size > $size) { $log .= "Image was too big "; } } //Checks if file is an image if (($img1_type == $cert1) or ($img1_type == $cert2) or ($img1_type == $cert3)) { if (($img1_type == $cert1) or ($img1_type == $cert2)) { $img1_name = $random1 . ".jpeg"; } if ($img1_type == $cert3) { $img1_name = $random1 . ".gif"; } @copy($img1, "$abpath/$img1_name") or $log .= "Couldn't copy image to the server "; if (file_exists("$abpath/$img1_name")) { $log .= "Image was uploaded successfully "; } } else { $log .= "File was not an image "; } } } ?> Upload report

Upload report: <? echo "$log"; ?>

index | uploader

Can not use a period when using double quotes. Either change to single quote or add a \ before the period.
For this script I would think it would be simpler to use a relative directory like so:

Instead of checking for both true or false you could use if else if statement.
The first if statement checks to see if a file has been selected, so shouldn’t the string being checked be $img1
I am assuming $img1 is the string being passed from the form, which probably should be $img1 = $_POST[‘nameoffieldinform’];
Again assuming your using post not get.
Hope some of this helps, since I can’t test it with part of it missing(mainly the form).

My website

You can use a period within double quotes as a general rule. The only place you don’t want to use a period (as a period) is in an ereg or preg_match-style.

Original poster: make sure your form enctype is set to “multipart/form-data”. Also, if I recall correctly, the max upload size is around 8M. Not sure if you’re running into that or not.


And actually, I don’t see how img1 is getting set. If that’s your variable name from the form, you’ll need to do something like $_FILES[‘tmp_name’][‘img1’] or whatever. I can’t recall the variable syntax right offhand. Your file data is in the $_FILES array.


Thanks for the help so far guys, I’ll have another play around later and let you guys know how it goes :slight_smile:

Sorry I forgot the form in my original post, here it is:


Image to upload:

The input name is not the same as a string with the same same. Like I mentioned earlier it would be $img1=$_POST[‘img1’];
To keep from getting confused it would be better if they had different names.
Then your if statement can check $img1 to see if it is set.

My website

As a few people have mentioned, you do not define the $img1_name variable. Because of that, it is null (or simply “”).

So, when this if() is executed, it is always true:

[quote]if ($img1_name == “”) {
$log .= “No file selected for upload
That explains why you’re getting that bug where it always gives you the error. Everyone else has some pretty good suggestions for how to fix it :wink:

Heres the documentation on for file uploads:

you’ll want to use:


I attached some codes here.

if (file_exists($_FILES[“file”][“name”])){
echo $_FILES[“file”][“name”]." already exists.";
move_uploaded_file($_FILES[“file”][“tmp_name”], $_SERVER[‘DOCUMENT_ROOT’]."/upload/".$_FILES[“file”][“name”]);
echo “Stored in: “.”/upload/”.$_FILES[“file”][“name”];

when you try to upload a file “file”, you should get the file name using $_FILE.

the folder path should start from the same foler where your php file are stored.

if you use rand, you may have the same name for the two files. i always use this “mktime(gmdate(“Y m d H:i:s”))” to make sure there is no same name.

Save [color=#CC0000]$97[/color] (max discount) on dreamhost by using promo code: [color=#CC0000]97CRAZY[/color].