Jump to content
Sign in to follow this  
brainonastick

Getting file upload code to work

Recommended Posts

Hi there,

I have been trying to get a file upload script to work. I think the problem is with the file path of the destination. If this script is on the web server, and there is a folder called uploads, is that enough for the file path? Code follows:

<?php

if ((($_FILES["file"]["type"] == "image/gif")

|| ($_FILES["file"]["type"] == "image/jpeg")

|| ($_FILES["file"]["type"] == "image/pjpeg"))

&& ($_FILES["file"]["size"] < 20000))

{

if ($_FILES["file"]["error"] > 0)

{

echo "Return Code: " . $_FILES["file"]["error"] . "

";

}

else

{

echo "Upload: " . $_FILES["file"]["name"] . "

";

echo "Type: " . $_FILES["file"]["type"] . "

";

echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb

";

echo "Temp file: " . $_FILES["file"]["tmp_name"] . "

";

if (file_exists("/uploads/" . $_FILES["file"]["name"]))

{

echo $_FILES["file"]["name"] . " already exists. ";

}

else

{

move_uploaded_file($_FILES["file"]["tmp_name"],

"uploads/" . $_FILES["file"]["name"]);

echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];

}

}

}

else

{

echo "Invalid file";

}

?>

Any advice gratefully received.

Share this post


Link to post
Share on other sites

It should actually work fine though your if file exists check is off - you shouldn't be inserting a backslash in front of your file path if the upload folder resides in the same directory as your script... Anyway, just give this a shot, and if it doesn't work shoot back.

Another likely issue is that you don't have write access to the folder. If you're on linux or mac, give the webserver write rights.... if you're not sure, just change the permissions to 777 temporarily and cut them back gradually if it works. If you're on windows, then the IUSR account will need to have write rights...


<?php

if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000))

{

	if ($_FILES["file"]["error"] > 0)

	{

		echo "Return Code: " . $_FILES["file"]["error"] . "

";

	}

	else

	{

		echo "Upload: " . $_FILES["file"]["name"] . "

";

		echo "Type: " . $_FILES["file"]["type"] . "

";

		echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb

";

		echo "Temp file: " . $_FILES["file"]["tmp_name"] . "

";

		if (file_exists(dirname(__FILE__)."/uploads/" . $_FILES["file"]["name"]))

		{

			echo $_FILES["file"]["name"] . " already exists. ";

		}

		else

		{

			move_uploaded_file($_FILES["file"]["tmp_name"],

			dirname(__FILE__)."/uploads/" . $_FILES["file"]["name"]);

			echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];

		}

	}

	}

else

{

	echo "Invalid file";

}

?>

Cheers.

Edited by Guest

Share this post


Link to post
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
Sign in to follow this  

  • Who Viewed the Topic

    1 member has viewed this topic:
    Will Xu 
×

Important Information

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