Jump to content

Error message on file upload... help?


simcoweb

Recommended Posts

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:

[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;
        }
      }[/code]

Here's the form:

[code]
<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>[/code]

and here's the dang error message:

[code]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[/code]

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 :)
Link to comment
https://forums.phpfreaks.com/topic/15841-error-message-on-file-upload-help/
Share on other sites

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?:

[code=php:0]$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;
}
}[/code]

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...
sanfly, I inserted that code and got this error:

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

which is this line:

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

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

public_html/test.php
public_html/uploads

[quote author=simcoweb link=topic=102110.msg404839#msg404839 date=1154045310]
sanfly, I inserted that code and got this error:

[quote]Parse error: parse error, unexpected '{' in /home/xxxxx/public_html/test.php on line 38[/quote]
[/quote]
[code]<?php

38 if(!is_dir($uploadDir)){

?>[/code]
ahhh yes, the simplest things. Ok, added the additional right parentheses and now get this:

[quote]ERROR: ../uploads/ is not a valid directory[/quote]

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.
Sorry about the missing ')', I usually provide all my scripts with a disclaimer.

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

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

to this:

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

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

[code]$uploadDir = "/uploads/";[/code]

to this:

[code]$uploadDir = "uploads/";[/code]

And just delete the whole directory check thing

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

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

exit();
}[/code]

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
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:

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

That's this line:

[quote]echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'><img src="<?php echo $image; ?>"></td>";[/quote]
Your getting the error because the code is already within php tags

you have this:

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

change it to this:

[code]echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'><img src=\"$image\"></td>";[/code]
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:

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

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

[code]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;[/code]

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

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

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.templatedepot.com/layout-test-form.htm.

It's basically a test in file uploading and then parsing the data for the screen. :)
Okay,there are two reasons youre having a problem

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

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

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

[code]<img src=\"$image\">[/code]

so, change the line:

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

to:

[code=php:0]echo "<tr><td rowspan='3' bgcolor='#303030'><font face='Verdana' size='3'>$image</td>";[/code]

Secondly, the image name you are getting is

[quote]'uploads/.gif[/quote]

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

First, delete this line

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

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

[code=php:0]$image = "<img src='$uploadDir$newFile' border='0'/>";[/code]

and THEN put your HTML to show the image

Let me know how it goes....

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.