0
確定首先,我是新來的,所以如果我沒有正確發佈,請告訴我。我在這個網站上進行了廣泛的搜索,但沒有發現我的問題。上傳多個文件,用表格數據重命名
我有一個php表單和上傳器,應該上傳多個文件並根據表單名稱字段中的響應以及上傳日期重命名文件(將名稱&日期添加到原始文件名) 。
一切正常EXCEPT:只有一個文件上傳,而不是多個文件。當我寫這篇文章的時候,它確實有效,但是最近我已經搞砸了。
有人能告訴我我哪裏出錯了嗎? 謝謝。
這裏是表單代碼:
<?php
// make a note of the current working directory relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
// location of the upload handler
$uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.processor.php';
// max file bytes (2mb)
$max_file_size = 2097152;
// echo
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<title>Upload Your Awesome Images!</title>
</head>
<body>
<form id="Upload" action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">
<span>Step #2</span>
<h1>
Upload Your Images!
</h1>
<div style="border:#999 1px solid;padding:5px">
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
</p>
<p> Photographer's Name: <input type="text" name="photogname" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>"> </p>
<p>
<label for="file">File to upload:</label>
<input id="file" type="file" name="file[]" class="tag" multiple/>
</p>
<p>
<label for="submit">Press to...</label>
<input id="submit" type="submit" name="submit" value="Upload me!">
</p>
</div>
<p>Max file size= 2mb</p>
<p>Max number of files uploadable = 20</p>
<p>*Important Note: When submitting photos, please be sure that you are the owner of the images, or that you have explicit permission from the owner to use them. All copyrights will be verified before winners are announced. </p>
</form>
</body>
</html>
這裏是處理:
<?php
//current local
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
//locations/dirs
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . '../images/PhotoContest/';
$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.form.php';
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php';
$fieldname = 'file';
// upload
// possible upload errors (more?)
$errors = array(1 => 'php.ini max file size exceeded',
2 => 'html form max file size exceeded',
3 => 'file upload was only partial',
4 => 'no file was attached');
// check the upload form was actually used. no fancy injection stuff
isset($_POST['submit'])
or error('woah. you can\'t upload images without the form', $uploadForm);
// check for standard uploading errors
($_FILES[$fieldname]['error'] == 0)
or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
// check that the file was an HTTP upload, not a sneaky pete
@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
or error('not an HTTP upload', $uploadForm);
// validation...
// should run a check to make sure the upload is an image
@getimagesize($_FILES[$fieldname]['tmp_name'])
or error('only image uploads are allowed', $uploadForm);
//name this file. If not, name by time (remove time function when name works right)
$now = date('m-d-Y');
$photogname = ($_POST['photogname']);
while(file_exists($uploadFilename = $uploadsDirectory.$photogname.$now.'-'.$_FILES[$fieldname]['name']))
{
$now++;
}
// move the file to final dir (& check perm)
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);
// sucess
header('Location: ' . $uploadSuccess);
// error handler
function error($error, $location, $seconds = 5)
{
header("Refresh: $seconds; URL=\"$location\"");
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
'"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
'<html lang="en">'."\n".
' <head>'."\n".
' <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
' <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
' <title>Upload error</title>'."\n\n".
' </head>'."\n\n".
' <body>'."\n\n".
' <div id="Upload">'."\n\n".
' <h1>Upload failure</h1>'."\n\n".
' <p>An error has occured: '."\n\n".
' <span class="red">' . $error . '...</span>'."\n\n".
' The upload form is reloading</p>'."\n\n".
' </div>'."\n\n".
'</html>';
exit;
} // end error handler
?>
「多個」不應該在那裏,它是從一個不同的形式遺留下來,我從原來的複製領域。但是這也不應該引起任何問題,所以我也不會擔心。我不完全理解如何在沒有完成的情況下將該PHP實現到我的代碼中; y重寫所有內容。我還在學習,對不起。謝謝。 – user2712559
您必須更改與上傳文件相關的部分。檢查我的編輯。 – MahanGM
至此感謝您的所有幫助,但目前還無法解決。 PHP工作正常(正確上傳單個文件,正確命名文件),但輸入導致它失敗(沒有文件上傳)。 – user2712559