Jump to content


Photo

Error message on file upload... help?


  • Please log in to reply
17 replies to this topic

#1 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 27 July 2006 - 11:05 PM

I have a simple form (code below) just to test the file upload snippet. No matter what I do I get an error message while it's trying to write the temp name and transfer the image to the /uploads folder. Here's the code:

$image = "<img src='../uploads". $result['ID']. ".gif' border='0'/>";
// we have to deal with the image upload first
if ( move_uploaded_file ($_FILES['image'] ['tmp_name'],
       "uploads/{$_FILES['uploadFile'] ['name']}")  )
      {  print '<p> The file has been successfully uploaded </p>';
       }
else
      {
        switch ($_FILES['uploadFile'] ['error'])
         {  case 1:
                   print '<p> The file is bigger than this PHP installation allows</p>';
                   break;
            case 2:
                   print '<p> The file is bigger than this form allows</p>';
                   break;
            case 3:
                   print '<p> Only part of the file was uploaded</p>';
                   break;
            case 4:
                   print '<p> No file was uploaded</p>';
                   break;
         }
       }

Here's the form:

<html>
<title>This is a test PHP page</title>
<head></head>
<body>
<form action='test.php' method='POST' enctype="multipart/form-data">
<input type="hidden" value="MAX_FILE_SIZE" value="100000" />
<table width="600" border="0" align="center">
<tr><td><font class="bodytext">Name:</td><td><input type="texbox" size="20" name="name"></td></tr>
<tr><td><font class="bodytext">Title:</td><td><input type="textbox" size="20" name="title"></td></tr>
<tr><td><font class="bodytext">Business Summary</td><td><textarea cols="20" rows="6" name="summary"></textarea></td></tr>
<tr><td colspan="2"><font class="bodytext">Select an image from your hard drive to upload for your profil</td></tr>
<tr><td colspan="2"><input type="file" size="20" name="image"/>
<tr><td colspan="2"><input type="submit" value="Send Profile"></form></td></tr>
</table>
</body>
</html>

and here's the dang error message:

Warning: move_uploaded_file(uploads/): failed to open stream: Is a directory in /home/xxxxxx/public_html/test.php on line 11

Warning: move_uploaded_file(): Unable to move '/tmp/phpdvlTi9' to '../uploads' in /home/xxxxxx/public_html/test.php on line 11

It's driving me nuts. The permissions on the /uploads file is 777. The permissions on the /tmp folder which is located below the public area is 700. The server configuration will not allow it to be changed. That may be the problem but I don't know for sure until another set of eyes peer at the code to make sure it's not my bone headed code :)

#2 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 27 July 2006 - 11:16 PM

While I have a close look at this, try adding a preceeding slash to the upload dir

move_uploaded_file($_FILES['image']['tmp_name'], "/uploads/{$_FILES['uploadFile']['name']}")


If you're not part of the solution, you're part of the precipitate

#3 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 27 July 2006 - 11:29 PM

Hi, thanks for the response. I should've mentioned that I've tried every conceivable method of entering that directory:

../uploads
../uploads/
/uploads/
uploads
uploads/

All produce the exact same error except for the mention of the directory :(

#4 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 27 July 2006 - 11:39 PM

What is the path of the upload directory relative to the directory where the script that executes the upload is located?

Try this out, do you get the same error?:

$image = "<img src='../uploads". $result['ID']. ".gif' border='0'/>";

$uploadDir = "/uploads/";
// Check if directory exists
if(!is_dir($uploadDir){
	echo "ERROR: $uploadDir is not a valid directory<br><br>";
	exit();
}
$srcFile = $_FILES['image']['tmp_name'];
$newFile = $_FILES['uploadFile']['name'];


if (move_uploaded_file($srcFile, "$uploadDir$newFile")){
	print '<p> The file has been successfully uploaded </p>';
}
else{
	switch ($_FILES['uploadFile'] ['error']){  
		case 1 : print '<p> The file is bigger than this PHP installation allows</p>'; break;
		case 2 : print '<p> The file is bigger than this form allows</p>'; break;
		case 3 : print '<p> Only part of the file was uploaded</p>'; break;
		case 4 : print '<p> No file was uploaded</p>'; break;
	}
}


If you're not part of the solution, you're part of the precipitate

#5 Drumminxx

Drumminxx
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts
  • LocationUSA

Posted 27 July 2006 - 11:50 PM

shouldn't this:

move_uploaded_file ($_FILES['image'] ['tmp_name'], "uploads/{$_FILES['uploadFile'] ['name']}") 

be:

move_uploaded_file ($_FILES['image'] ['tmp_name'], "uploads/{$_FILES['image] ['name']}")

I do not see a file field named uploadFile on your form...

:: gucci.com ::
:: blockbuster.com ::

--------------------

#6 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 28 July 2006 - 12:08 AM

sanfly, I inserted that code and got this error:

Parse error: parse error, unexpected '{' in /home/xxxxx/public_html/test.php on line 38


which is this line:

38 if(!is_dir($uploadDir){
39 echo "ERROR: $uploadDir is not a valid directory<br><br>";
40 exit();
41 }

the 'test.php' script in relation to the uploads folder is as so:

public_html/test.php
public_html/uploads



#7 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 28 July 2006 - 12:18 AM

sanfly, I inserted that code and got this error:

Parse error: parse error, unexpected '{' in /home/xxxxx/public_html/test.php on line 38

<?php

38 if(!is_dir($uploadDir)){

?>

PHP Ninja

#8 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 28 July 2006 - 12:23 AM

ahhh yes, the simplest things. Ok, added the additional right parentheses and now get this:

ERROR: ../uploads/ is not a valid directory


Ok, before I made this reply I tried:

/uploads
/uploads/
../uploads
uploads/

all return the same error. Trust me, uploads IS a valid directory. And, the permissions are set at 777 so I know it's writeable.

#9 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 28 July 2006 - 12:31 AM

What is the actual domain name?

If you don't feel comfortable posting that, you can email it to me. Also, can you access the "/uploads" directory publically from your browser?
PHP Ninja

#10 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 28 July 2006 - 12:40 AM

no problem, it's http://www.templatedepot.com/uploads/

I placed a dummy index page in there so it won't show the directory tree.



#11 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 28 July 2006 - 02:05 AM

Sorry about the missing ')', I usually provide all my scripts with a disclaimer.

Drumminxx is also correct about the naming issue, change this line:

$newFile = $_FILES['uploadFile']['name'];

to this:

$newFile = $_FILES['image']['name'];

I was also wrong about the preceeding slash, change this line:

$uploadDir = "/uploads/";

to this:

$uploadDir = "uploads/";

And just delete the whole directory check thing

// Check if directory exists
if(!is_dir($uploadDir){

echo "ERROR: $uploadDir is not a valid directory<br><br>";

exit();
}

If it still doesnt work and you dont mind, please post the location of the file that is executing the script, so we can make sure the relative path is correct
If you're not part of the solution, you're part of the precipitate

#12 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 28 July 2006 - 03:02 AM

Yaaaaaaay! That did the trick! Ok, i'm taking notes. Thanks!

One more question. HOW would I call this image file into say a table cell? I've tried using <img src="<? echo $image; ?>"> but that produced some other weird error about an 'unexpected ?'. That was before we fixed this code, though. Suggestions?

Update: Just tried it again and get this:

Parse error: parse error, unexpected '?' in /home/xxxxxx/public_html/test.php on line 61


That's this line:

echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'><img src="<?php echo $image; ?>"></td>";



#13 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 28 July 2006 - 03:49 AM

Your getting the error because the code is already within php tags

you have this:

echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'><img src="<?php echo $image; ?>"></td>";

change it to this:

echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'><img src=\"$image\"></td>";

If you're not part of the solution, you're part of the precipitate

#14 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 28 July 2006 - 05:10 AM

Ok, that makes perfect sense. I think I saw it done that way in some other post. Anyway, error message is gone but the image doesn't show. Just a broken link to it.  If I pull up the pic's properties via my browser I get this location:

http://www.templatedepot.com/%3Cimg%20src='uploads/.gif'%20border='0'/%3E


Something is not parsing correctly obviously.

#15 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 28 July 2006 - 11:38 AM

What do you get if you just echo $image

eg

echo "$image";

If you're not part of the solution, you're part of the precipitate

#16 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 28 July 2006 - 04:54 PM

I'm parsing all this HTML under one echo statement which includes the $image tag utilizing the way you've specified it:

echo <<<HTML
<head>
<title>Your Profile</title>
<link rel="stylesheet" type="text/css" href="tdepot.css">
</head>
<body>
<table border='0' bgcolor='#FFFFFF' cellpadding='2' cellspacing='3' border='0' width='650'>
<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'><img src="$image"></td>
<td bgcolor='C0C0C0'><font class='bodytext'><i>$name</i></td></tr>
<tr><td bgcolor='#808080'><font class='bodytext'>$title</td></tr>
<tr><td bgcolor='#E9F2E4'><font class='bodytext'>$info</td></tr></table></body>
HTML;

After the page is parsed I get this as the image location:

http://www.templatedepot.com/%3Cimg%20src='uploads/.gif'%20border='0'/%3E

The <'s are getting replaced somehow in the parsing of the code. Plus, I don't see any reference to the actual picture ID in the img src that's being pulled.


You can view the form and try it yourself if you want: http://www.templated...-test-form.htm.

It's basically a test in file uploading and then parsing the data for the screen. :)

#17 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 29 July 2006 - 08:39 PM

Okay,there are two reasons youre having a problem

First and most obviously, when you define "$image", you have this

$image = "<img src='../uploads". $result['ID']. ".gif' border='0'/>";

then when you call $image, you are putting it in the img src tag again

<img src=\"$image\">

so, change the line:

echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'><img src=\"$image\"></td>";

to:

echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'>$image</td>";

Secondly, the image name you are getting is

'uploads/.gif


The image I tried uploading wasnt even a gif, and there is no file name.

First, delete this line

$image = "<img src='../uploads". $result['ID']. ".gif' border='0'/>";

Then BELOW where you have the code to upload the file, put this

$image = "<img src='$uploadDir$newFile' border='0'/>";

and THEN put your HTML to show the image

Let me know how it goes....
If you're not part of the solution, you're part of the precipitate

#18 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 31 July 2006 - 11:00 PM

Sorry, was gone for 3 days. Thanks for the response and further explanation. I see clearly now what you're talking about.

I made the code changes and the image now shows up properly. Thanks!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users