Jump to content

Moving Uploaded File


chronister

Recommended Posts

I am trying to make a simple file upload page. It will not move the file properly. It only spits out the error message. Here's the code.

<?php
if($_POST['submit'])
{	 
if($_FILES['file_upload']['tmp_name'])
{
	$file=$_FILES['file_upload']['tmp_name'];
		if(is_uploaded_file($file)) 
		{
			$name=$_FILES['file_upload']['name'];
			$size=fsize_convert($_FILES['file_upload']['size']);
			$uploaddir = $_SERVER['DOCUMENT_ROOT']."/members/docs/";
			$uploaddir.=$name;

			if(move_uploaded_file($_FILES['file_upload']['tmp_name'], $uploaddir))
			{
			  echo "Documents Updated!";
			}
			else
			{
			  echo "There was a problem when uploading the new file";
			  
			}

		}
}
}
else
{
?>
<form action="<?=$_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="50%" border="0" align="center" cellpadding="5" cellspacing="0">
  <tr>
    <td>Title</td>
    <td>
      <input name="title" type="text" id="title" />
      </td>
  </tr>
  <tr>
    <td>Description</td>
    <td>
      <textarea name="description" cols="35" rows="3" id="description"></textarea>
      </td>
  </tr>
  <tr>
    <td>File</td>
    <td><input type="file" name="file_upload" id="file_upload" /></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td colspan="2" align="center">
      <input name="submit" type="submit" id="submit" value="Submit" />
    
    </td>
  </tr>
</table>
</form>
<br /><br /><br /><br />

<?php
};

if($_POST['submit'])
{

echo '<br><br><pre>';
print_r($_FILES);
echo '</pre>';
}

?>

 

And here is what it outputs.

There was a problem when uploading the new file

 

Array

(

    [file_upload] => Array

        (

            [name] => ipconfig.txt

            [type] => application/x-txt

            [tmp_name] => /tmp/phpBehAOy

            [error] => 0

            => 1155

        )

 

)

 

 

Here is an echo of $uploaddir:

 

/var/www/test/members/docs/ipconfig.txt

 

Does anyone see a problem here?? I chmod'ed the docs directory to 0755.

 

What am I doing wrong here??

 

Thanks in advance

Link to comment
Share on other sites

Well, that fixed it apparently. I thought that the move_uploaded_file ran under root permissions, hence it only needed to be owner writable, and just readable for group and world.

 

I am really new to linux and real file permissions, but I thought running a folder in 777 is a bad thing? Am I wrong here?

 

Thanks

Link to comment
Share on other sites

Here is a little primer chart that I just made for you :)

----------------------------------|

|          Read | Write | Execute    |

|------|-------------|------------|

|Owner|  x  |    x    |        x      |

|------|-----|-------|------------|

|Group|    x  |    x    |        x      |

|------|-----|-------|------------|

|Other |  x  |    x    |        x      |

|---------------------------------|

 

That is a chmod 777

 

What you had at 755 was

 

----------------------------------|

|          Read | Write | Execute    |

|------|-------------|------------|

|Owner|  x  |    x    |        x      |

|------|-----|-------|------------|

|Group|    x  |        |        x      |

|------|-----|-------|------------|

|Other |  x  |        |        x      |

|---------------------------------|

 

The writing part I think was the problem.

 

Try changing to 775(Group write) or 757(Other write)

 

Chmod 777 isnt the best, its the most open. So try and contain it as much as possible with other peoples access.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.