PHP upload failing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jiggens
Forum Commoner
Posts: 37
Joined: Sun Jul 08, 2007 4:49 pm

PHP upload failing

Post by jiggens »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


am trying to upload an image using a html form and PHP and it seems to be failing at the move_uploaded_file i get a returned message of here was an error uploading the file, please try again!

I want to have this

Code: Select all

#Start the PHP session
	session_start();

#Variables set to log into SQL database
	$SQLusername = 'xxxx';
	$SQLpassword = 'xxx';
	$SQLdbname = 'xxxxx';
	$db = mysql_connect("localhost", $SQLusername, $SQLpassword) or print mysql_error ();
	mysql_select_db($SQLdbname) or print mysql_error ();

#Checks to see if user is logged in
	if($_SESSION['sesLogIn'] != 1) {
		header("Location: http://www.domain.com/admin/");
		exit();
	}
	
$ID = $_GET['ID'];
$siteID = $_GET['siteID'];

$uploaddir = '/homes/html/images/Temp/';
$uploadfile = $uploaddir . basename($_FILES['newMap']['name']);
$uploadfile2 = $uploaddir . basename($_FILES['newLogo']['name']);

#upload image function	
		function createImage($imgname,$maxh,$maxw,$newname){
		  list($width, $height) = getimagesize($imgname);
		  if ($width < $maxw && $height < $maxh) {
		  	$new_height = $height;
			$new_width = $width;
		  } else {
			$ratio = ($width/$height);
			$new_height = sqrt(10000/$ratio);
			$new_width = $width * ($new_height / $height);
		  } 
		  $image_p = imagecreatetruecolor($new_width, $new_height);
		  $image = imagecreatefromjpeg($imgname);
		  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);  
			//Grab new image
			ob_start();
			ImageJPEG($image_p);
			$image_buffer = ob_get_contents();
			ob_end_clean();
			ImageDestroy($image_p);
			//Create temporary file and write to it
			$fp = tmpfile();
			fwrite($fp, $image_buffer);
			rewind($fp);
		}


		
//#Resize Image based on maximum width and maximum height
function resizeBoth($imgname,$maxh,$maxw,$newname) {
		  list($width, $height) = getimagesize($imgname);
		  if ($width < $maxw && $height < $maxh) {
		  	$new_height = $height;
			$new_width = $width;
		  } if ($width > $height) {
			$new_height = $height * ($maxw / $width);
			$new_width = $maxw;
		  } else {
			$new_height = $maxh;
			$new_width = $width * ($maxh / $height);
		  }
		  $image_p = imagecreatetruecolor($new_width, $new_height);
		  $image = imagecreatefromjpeg($imgname);
		  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);  
			//Grab new image
			ob_start();
			ImageJPEG($image_p);
			$image_buffer = ob_get_contents();
			ob_end_clean();
			ImageDestroy($image_p);
			//Create temporary file and write to it
			$fp = tmpfile();
			fwrite($fp, $image_buffer);
			rewind($fp);
 }

if(array_key_exists('uploadNewLogo', $_POST)) {
		if ($ID != "" && $ID != "new") {
					if(move_uploaded_file($_FILES['newLogo']['tmp_name'], $uploadfile2)) {
					echo "The file ". basename($_FILES['newLogo']['tmp_name'][name]). "has been uploade";
			}else{
				echo "There was an error uploading the file, please try again!";
     }		
		}		
}


form enctype="multipart/form-data" action="<? print $_SERVER['PHP_SELF'] . "?ID=" . $ID ?>" method="POST">
		           
                  </tr>
                  <tr>
                    <td align="right" valign="top"><div align="right">Logo:</div></td>
                    <td align="left" valign="top"><?
						if ($ID != "" && $ID != "new") { 
						if (file_exists("/homes/html/images/browse/logos/" . $ID . ".jpg")) {
							print "<img src='http://homes.pacificscene.com/images/browse/logos/" . $ID . ".jpg' />"; 
							print "<input type='submit' name='deleteImage' value='Delete Logo' />";
						  print "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"30000\" />";
							print "<br />";
						}
						print "<input name='newLogo' type='file' size='40' /><input type='submit' name='uploadNewLogo' value='Upload' />";
						}
					
					?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Are you sure you have the proper permissions in the $uploaddir folder?
jiggens
Forum Commoner
Posts: 37
Joined: Sun Jul 08, 2007 4:49 pm

Post by jiggens »

Yes i am sure, i checked the permissions and i made sure that it was in the correct directory. I am on a windows hosting. Unless you think i am missing something for permissions?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You could start by telling me what the permissions for the folder are. 0777 is guaranteed to work.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please try

Code: Select all

if(array_key_exists('uploadNewLogo', $_POST)) {
  if ($ID != "" && $ID != "new") {
    error_reporting(E_ALL);
    ini_set('display_errors', true);

    echo "Debug: move_uploaded_file({$_FILES['newLogo']['tmp_name']}, {$uploadfile2} <br />\n";

    is_uploaded_file($_FILES['newLogo']['tmp_name']) or die('!is_uploaded_file()');
    is_readable($_FILES['newLogo']['tmp_name']) or die('!is_readable(tmp_name)');
    is_dir(dirname($uploadfile2)) or die('!is_dir('.dirname($uploadfile2).')');
    is_writable(dirname($uploadfile2)) or die('!is_writable('.dirname($uploadfile2).')');
    if(move_uploaded_file($_FILES['newLogo']['tmp_name'], $uploadfile2)) {
      echo "The file ". basename($_FILES['newLogo']['tmp_name'][name]). "has been uploaded";
    }else{
      echo "There was an error uploading the file, please try again!";
    }
  }
}
jiggens
Forum Commoner
Posts: 37
Joined: Sun Jul 08, 2007 4:49 pm

Post by jiggens »

this is what was returned

Debug: move_uploaded_file(d:\Inetpub\wwwroot\temp\phpC3.tmp, /homes/html/images/Temp/avatar60.jpg
!is_dir(/homes/html/images/Temp)


and no file uploaded as well?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

jiggens wrote:!is_dir(/homes/html/images/Temp)
There is no directory /homes/html/images/Temp ?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

It means your $uploaddir doesn't exist.
jiggens
Forum Commoner
Posts: 37
Joined: Sun Jul 08, 2007 4:49 pm

Post by jiggens »

but there is a directory its under my wwwroot folder it goes Inetpup/wwwroot/homes/html/images/temp

i see it in my file structure is it because its another website and doesn't fall under the same directory as html/admin/pshomes/ ?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If you pass a absolute local path to one of php's filesystem function it's not relative to Inetpup.
On win32 /homes/html/images/Temp means c:/homes/html/images/Temp or d:/homes/html/images/Temp or x:/homes/html/images/Temp
not c:/Inetpup/homes/html/images/Temp
jiggens
Forum Commoner
Posts: 37
Joined: Sun Jul 08, 2007 4:49 pm

Post by jiggens »

So i get this after i have changed it to Inetpub but the file isnt uploaded ?


Debug: move_uploaded_file(d:\Inetpub\wwwroot\temp\phpFA.tmp, /Inetpub/wwwroot/homes/html/images/temp/avatar60.jpg

Notice: Use of undefined constant name - assumed 'name' in D:\Inetpub\wwwroot\html\admin\pshomes\edit-communities.php on line 152
The file has been uploaded
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$_FILES['newLogo']['tmp_name'][name]
either

Code: Select all

$_FILES['newLogo']['tmp_name']
or

Code: Select all

$_FILES['newLogo']['name']
jiggens
Forum Commoner
Posts: 37
Joined: Sun Jul 08, 2007 4:49 pm

Post by jiggens »

changed to 'name'

now i get

Debug: move_uploaded_file(avatar60.jpg, /Inetpub/wwwroot/homes/html/images/temp/avatar60.jpg
!is_uploaded_file()
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You have changed $_FILES['newLogo']['tmp_name'] to $_FILES['newLogo']['name']
Why?
jiggens
Forum Commoner
Posts: 37
Joined: Sun Jul 08, 2007 4:49 pm

Post by jiggens »

I thought that is what you suggested my mistake. I appreciate all the help your give me as well.
Post Reply