php上传文件,在接受文件move_uploaded_file()的时候一直失败

最近在弄文件上传到服务器,cgi是用php处理。




service代码如下:

<?php

define('ROOT',dirname(__FILE__).'/');

  if ($_FILES["file"]["error"] > 0)
  {
		echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  }
  else
    {
		echo "Upload: " . $_FILES["file"]["name"] . "<br />";
		echo "Type: " . $_FILES["file"]["type"] . "<br />";
		echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
		echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

		if (file_exists("upload/" . $_FILES["file"]["name"]))
		{
			echo $_FILES["file"]["name"] . " already exists. ";
		}
		else
		{
			if(is_uploaded_file($_FILES['file']['tmp_name'])){
				$stored_path = ROOT.'/upload/'.basename($_FILES['file']['name']);
				
				if(move_uploaded_file($_FILES['file']['tmp_name'],$stored_path)){
					echo "Stored in: " . $stored_path;
				}else{
					echo 'Stored failed:file save error';
				}
			}else{
				echo 'Stored failed:no post ';
			}
		 }
    }
?>

注意:如果没有 ROOT全路径的话,就会上传失败,切记。

$stored_path = '/upload/'.basename($_FILES['file']['name']);


正确写全路径

$stored_path = ROOT.'/upload/'.basename($_FILES['file']['name']);



展开阅读全文

没有更多推荐了,返回首页