Jump to content

PHP Upload Script not Moving File! Please Help


evo4ever

Recommended Posts

Hey ppl its my first post here so here goes...

Me and two of my friends are creating a website called Souljaz Studios and Im in the middle of making the Image Upload script and Im having problems with it without errors. Heres my code...

function upload_image()
{
        // chmod'd to eliminate any permission errors...
	chmod("/gallery/", 777);
	
	if($_FILES["file"]["error"] > 0)
	{
		print "Error: " . $_FILES["file"]["error"] . "<br>";
	}
	else
	{

                // Display the $_FILES array. Everything checks out
		print_r(var_dump($_FILES));
		
		print "</br></br>";
		
		print "Upload: " . $_FILES["file"]["name"] . "<br>";
    	print "Type: " . $_FILES["file"]["type"] . "<br>";
   	 	print "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    	print "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
		
		if(file_exists("/gallery/" . $_FILES["file"]["name"]))
		{
			print $_FILES["file"]["name"] . " already exists. ";
		}
		else
		{
			move_uploaded_file($_FILES["file"]["tmp_name"], "/gallery/" . $_FILES["file"]["name"]);
			print "Image uploaded successfully!";
		}
	}
	
	if(is_uploaded_file($_FILES["file"]["tmp_name"]))
	{
		print "</br>File Uploaded"; 
	}
	else
	{
		print "</br>File NOT uploaded";
	}
	
}


The file gets uploaded to the windows/temp folder but it wont successfully move it to the /gallery/ folder within wwwroot/



upload_image.php file....

<?php
	include("inc/functions.inc.php");
	
	if(isset($_FILES["file"])) upload_image();
?>

<form action="upload_image.php" method="post" enctype="multipart/form-data" name="file">
  <p>
    <label for="image">Image</label>
    <input type="file" name="file" id="file">
  </p>
  <p>
    <input type="submit" name="upload" id="upload" value="Upload">
  </p>
</form>

Im running IIS by the way. Any help greatly appreciated! Thanks.

Few questions:

 

1. Is the function upload_image() located inside inc/functions.inc.php?

 

2. The gallery is a part of the web root directory or the project folder? 

 

3. When you include inc/functions.inc.php you have to point action of your html form (in case the function upload_image() is there) to the same file, so try that:

<?php

function upload_image()
{
        // chmod'd to eliminate any permission errors...
	chmod("gallery/", 777);
	
	if($_FILES["file"]["error"] > 0)
	{
		print "Error: " . $_FILES["file"]["error"] . "<br>";
	}
	else
	{

                // Display the $_FILES array. Everything checks out
		print_r(var_dump($_FILES));
		
		print "</br></br>";
		
		print "Upload: " . $_FILES["file"]["name"] . "<br>";
    	print "Type: " . $_FILES["file"]["type"] . "<br>";
   	 	print "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    	print "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
		
		if(file_exists("gallery/" . $_FILES["file"]["name"]))
		{
			print $_FILES["file"]["name"] . " already exists. ";
		}
		else
		{
			move_uploaded_file($_FILES["file"]["tmp_name"], "gallery/" . $_FILES["file"]["name"]);
			print "Image uploaded successfully!";
		}
	}
	
	if(is_uploaded_file($_FILES["file"]["tmp_name"]))
	{
		print "</br>File Uploaded"; 
	}
	else
	{
		print "</br>File NOT uploaded";
	}
	
}


1. Yes

2. Gallery is part of the web root folder

 

3. Ive got rid of the function upload_image() and put the code directly into the image_upload.php file, so im no longer using functions.inc.php.

 

New code as follows...

if(isset($_FILES["file"]))
{
	
	chmod("gallery/", 777);
	
	if($_FILES["file"]["error"] > 0)
	{
		print "Error: " . $_FILES["file"]["error"] . "<br>";
	}
	else
	{
		print_r(var_dump($_FILES));
		
		print "</br></br>";
		
		print "Upload: " . $_FILES["file"]["name"] . "<br>";
    	print "Type: " . $_FILES["file"]["type"] . "<br>";
   	 	print "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    	print "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
		
		if(file_exists("gallery/" . $_FILES["file"]["name"]))
		{
			print $_FILES["file"]["name"] . " already exists. ";
		}
		else
		{
			move_uploaded_file($_FILES["file"]["tmp_name"], "gallery/" . basename($_FILES["file"]["name"]));
			print "Image uploaded successfully!";
		}
	}
	
	if(is_uploaded_file($_FILES["file"]["tmp_name"]))
	{
		print "</br>File Uploaded"; 
	}
	else
	{
		print "</br>File NOT uploaded";
	}
}
?>

<form action="upload_image.php" method="post" enctype="multipart/form-data" name="file">
  <p>
    <label for="image">Image</label>
    <input type="file" name="file" id="file">
  </p>
  <p>
    <input type="submit" name="upload" id="upload" value="Upload">
  </p>
</form>

... And im still getting the same problem!!

Try this and give us the result if you get an error message:

 

inc/functions.inc.php

<?php

function upload_image()
{
        // chmod'd to eliminate any permission errors...
	chmod("/gallery/", 777);
	
	if($_FILES["file"]["error"] > 0)
	{
		print "Error: " . $_FILES["file"]["error"] . "<br>";
	}
	else
	{

                // Display the $_FILES array. Everything checks out
		print_r(var_dump($_FILES));
		
		print "</br></br>";
		
		print "Upload: " . $_FILES["file"]["name"] . "<br>";
    	print "Type: " . $_FILES["file"]["type"] . "<br>";
   	 	print "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    	print "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
		
		if(file_exists("/gallery/" . $_FILES["file"]["name"]))
		{
			print $_FILES["file"]["name"] . " already exists. ";
		}
		else
		{
			move_uploaded_file($_FILES["file"]["tmp_name"], "/gallery/" . $_FILES["file"]["name"]);
			print "Image uploaded successfully!";
		}
	}
	
	if(is_uploaded_file($_FILES["file"]["tmp_name"]))
	{
		print "</br>File Uploaded"; 
	}
	else
	{
		print "</br>File NOT uploaded";
	}
	
}

upload_image.php

<?php
        error_reporting(-1);
        
	include("inc/functions.inc.php");
	
	if(isset($_FILES["file"])) upload_image();
?>

<form action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="post" enctype="multipart/form-data" name="file">
  <p>
    <label for="image">Image</label>
    <input type="file" name="file" id="file">
  </p>
  <p>
    <input type="submit" name="upload" id="upload" value="Upload">
  </p>
</form>

EDIT: Make sure that the path to your web root directory is "C:\inetpub\wwwroot", inside the config file of your web server!

By the way, In linux (unix) if want to change the permission recursively you need to add a "--recursive" flag.

 

In php should be something like:

chmod("/somedir/ --recursive", 755);

// OR

chmod("/somedir/", 755, "--recursive");

But I'm not sure just test it ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.