Page 1 of 1

Uploading Files

Posted: Wed May 17, 2006 8:36 pm
by watson516
I have attempted to allow users to upload pictures to my website but I can not figure it out.

Here is what I currently have...

Code: Select all

if (isset($_GET['sat']))
{
	if(!empty($_FILES["userfile"]))
	{
		$dir = opendir("pictures/");
		$counter = 0;
		while($file = readdir($dir)){
   			if($file != '.' && $file != '..')
			{
      				$counter++;
   			}
		}
		closedir($dir);
    		$uploaddir = "/pictures/";
    			
		if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $uploaddir . $_FILES["userfile"]["$counter"]))
		{
			echo "File Uploaded";
		}else{
			echo "Error";
		}
	}
}
I would like it to count the number of files in pictures/ and name the uploaded file the number it comes up with.
When I try this I get the following warmings...
Warning: move_uploaded_file(/pictures/): failed to open stream: Permission denied in /home/watson516/public/www/adminpanel.php on line 235

Warning: move_uploaded_file(): Unable to move '/tmp/phpfC1gO1' to '/pictures/' in /home/watson516/public/www/adminpanel.php on line 235
I checked the folder permissions and as far as I can tell, all is good.

Posted: Wed May 17, 2006 8:42 pm
by watson516
Ok well I figured it out somewhat....

Code: Select all

if(!empty($_FILES["userfile"]))
		{
			$gallery = opendir("pictures/");
			$counter = 0;
			while($file = readdir($gallery)){
   				if($file != '.' && $file != '..')
				{
      					$counter++;
   				}
			}
			closedir($gallery);
    			$uploaddir = "pictures/";
    			
			if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $uploaddir . $counter . ".jpg"))
			{
				echo "File Uploaded";
			}else{
				echo $count;
				echo "Error";
			}
		}
Is there any good way to not have to include that last .jpg so it can support any image file?

Posted: Wed May 17, 2006 10:03 pm
by litebearer
I didn't test it, but should work.


insert this line...

Code: Select all

$ext = strtolower(strrchr($_FILES["userfile"]["tmp_name"], '.'));
between these lines...

Code: Select all

$uploaddir = "pictures/"; 

                
                        if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $uploaddir . $counter . $ext))

like so...

Code: Select all

$uploaddir = "pictures/"; 

               $ext = strtolower(strrchr($_FILES["userfile"]["tmp_name"], '.'));
                
                        if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $uploaddir . $counter . $ext))