Ankit Sachan Ankit Sachan - 1 month ago 13
PHP Question

php : unable to upload file to the server

hey I am not much of a PHP coder

I am using following to upload file to server acn any body help me whats wrong with this code

<?php

$uploaddir = './uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "http://iphone.zcentric.com/uploads/{$file}";
}

?>


Thanx in advance

Answer

I don't see anything wrong with the PHP code, though without an error it is difficult to tell what is happening.

Somethings which could cause uploads not to work, and which may not return errors:

  1. Ensure you have enctype="multipart/form-data in the form tag:

    <form enctype="multipart/form-data" action="__URL__" method="POST">

  2. Make sure PHP is accepting the input, by adjusting the following PHP ini variables:

    http://us.php.net/manual/en/ini.core.php#ini.post-max-size http://us.php.net/manual/en/info.configuration.php#ini.max-input-time http://us.php.net/manual/en/ini.core.php#ini.upload-max-filesize

  3. Finally, ensure that permissions are properly set for both the temp upload folder (http://us.php.net/manual/en/ini.core.php#ini.upload-tmp-dir) and the folder you are moving files to. If it is a Windows server you might also run into an inheritance issue which will require you to change the default upload directory.