[英]PHP File upload incomplete
我正在嘗試上傳圖片。 我創建了一個函數,該函數可以用受支持的文件類型替換不受支持的文件類型的擴展名。 (例如,“ honeydew.docx到honeydew.jpg”)
如果成功,則輸出將為“'Honeydew.docx已成功上傳並重命名為honeydew.jpg”。 我可以獲取“已成功上傳”部分,但無法顯示消息的“和已重命名”部分。 為什么會這樣?
在form.php中調用
<?php
use foundationphp\UploadFile;
$max = 50 * 1024;
$result = array();
if (isset($_POST['upload'])) {
require_once 'src/foundationphp/UploadFile.php';
$destination = __DIR__ . '/uploaded/';
try {
$upload = new UploadFile($destination);
$upload->setMaxSize($max);
$upload->allowAllTypes();
$upload->upload();
$result = $upload->getMessages();
} catch (Exception $e) {
$result[] = $e->getMessage();
}
}
?>
UploadFile.php中的函數處理程序
/*Function responsible for the output message*/
protected $newName;
protected function moveFile($file)
{
$filename = isset($this->newName) ? $this->newName : $file['name'];
$success = move_uploaded_file($file['tmp_name'], $this->destination . $filename);
if ($success) {
$result = $file['name'] . ' was uploaded successfully';
if (!is_null($this->newName)) {
$result .= ', and was renamed ' . $this->newName;
}
$result .= '.';
$this->messages[] = $result;
} else {
$this->messages[] = 'Could not upload ' . $file['name'];
}
}
/*Function to allow docs with all types of extensions to be displayed*/
protected $typeCheckingOn = true;
public function allowAllTypes($suffix = null)
{
$this->typeCheckingOn = false;
if (!is_null($suffix)) {
if (strpos($suffix, '.') === 0 || $suffix == '') {
$this->suffix = $suffix;
} else {
$this->suffix = ".$suffix";
}
}
}
enter code here
嘗試改變
if (!is_null($this->newName)) {
用
if (isset($this->newName)) {
當您在此檢查上方的代碼中使用了這種方法時。
雖然,我們只能預測錯誤的位置。 請發布整個UploadFile.php類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.