尝试新的文件上传方法

时间:2010-03-02 00:18:57

标签: php mysql

所以我决定将文件上传到Web服务器空间而不是mysql空间,但每次我尝试上传任何内容时都会出现以下错误:

    Warning: move_uploaded_file() [function.move-uploaded-file]: Filename cannot be empty in /home/speedycm/public_html/manageclient.php on line 240

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpq7cUA4' to '' in /home/speedycm/public_html/manageclient.php on line 240
Error uploading file

这是我使用的代码:

 <?php
                 if (array_key_exists('uploadfile',$_POST)) {

$uploadDir = '/upload';

$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;

// get the file extension first
$ext = substr(strrchr($fileName, "."), 1); 

// make the random file name
$randName = md5(rand() * time());

// and now we have the unique file name for the upload file
$filePath = $uploadDir . $randName . '.' . $ext;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$content = addslashes($filePath);
}

mysql_query("INSERT INTO tbl_accidentfiles (`client_id`,`name`,`type`,`size`,`path`)
VALUES ('$client_id', '$fileName', '$fileType', '$fileSize','$filePath')");

echo '<b>File Upload</b><p>Thank you. The file has been successfully uploaded.

<p><img src="resources/spacer.gif" alt="" width="300px" height="5px" /><p><o>

<i><u>Name:</u>&nbsp;' . $fileName . '<p><i><u>Size:</u>&nbsp;' . $fileSize . 'k' . '<p><u>Type:</u>&nbsp;' . $fileType . '</i><i><u>Path:</u>&nbsp;' . $filePath. '<p><p><p><img src="resources/spacer.gif" alt="" width="300px" height="5px" />';

关于我做错什么的任何想法?

1 个答案:

答案 0 :(得分:0)

  

警告:move_uploaded_file()[function.move-uploaded-file]:第240行/home/speedycm/public_html/manageclient.php中的文件名不能为空

这意味着$_FILES['userfile']['tmp_name']中的任何内容实际上都是空的,或者不存在。在print_r上运行var_dump$_FILES,看看实际上是什么。你有可能在某处犯错。