我正在尝试上传图片。我创建了一个函数,用一个支持的文件类型替换不受支持的文件类型的扩展。 (例如“honeydew.docx to honeydew.jpg”)
输出将是“'Honeydew.docx已成功上传,如果成功则重命名为honeydew.jpg”。我可以获得“已成功上传”部分,但无法显示“已重命名”部分消息。为什么会这样?
表格中的来电者.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
答案 0 :(得分:0)
尝试更改
if (!is_null($this->newName)) {
用
if (isset($this->newName)) {
正如您在上面的代码中使用这种方法一样。
虽然,我们只能预测错误的位置。请发布整个 UploadFile.php 课程。