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 = bas开发者_Python百科ename($_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
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:
Ensure you have
enctype="multipart/form-data
in the form tag:<form enctype="multipart/form-data" action="__URL__" method="POST">
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
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.
iF YOU WANT TO UPLOAD .pdf FILE TO LOCAL SERVER THEN USE THIS SIMPLE METHOD, Lets we are doing code here under Button Click Event...
if (isset($_POST['submit']))
{
if ( ($_FILES["file"]["type"] =="application/pdf"))
{
if (file_exists("C:/xampplite/htdocs/site/upload/" . $_FILES["file"]["name"]))
echo " This File is already exists in folder";
else
{
move_uploaded_file ($_FILES["file"]["tmp_name"],"C:/xampplite/htdocs/site/upload/" . $_FILES["file"]["name"]);
echo "File have been Stored in:-C:/xampplite/htdocs/site/upload/ " . $_FILES["file"]["name"];
}
}
}//end of click_event
could u pls publish what the error u get?Your code looks ok.Here the upload folder must he stay in the upper of the directory where you run the code.Then it should to work.if your script folder like this /test/script/abc.php then your uploads directory should be /test/uploads.
index.php
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload[]" id="fileToUpload" multiple="">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
upload.php
<?php
//$target_dir = "uploads/";
/*$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
*/
if(count($_FILES['fileToUpload']['name']) > 0)
{
$i=0;
while($i<count($_FILES['fileToUpload']['name']))
{
$filen = $_FILES["fileToUpload"]['name']["$i"];
$path = 'uploads/'.$filen;
$imageFileType = pathinfo($path,PATHINFO_EXTENSION);
if (file_exists($path)) {
echo "Sorry, file already exists.";
}else if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
else if(move_uploaded_file($_FILES["fileToUpload"]['tmp_name']["$i"],$path))
{
//echo "The file ". basename( $_FILES["fileToUpload"]["name"]["$i"]). " has been uploaded.";
$files=$_FILES["fileToUpload"]["name"]["$i"];
echo $files;?><img src="<?php echo $path;?>" style="width:200px;height:200px" alt="" >
<?php
}
$i++;
}
}
?>
精彩评论