我正在创建一个非常简单的管理面板,我希望能够上传文件,但这只是一个具有静态名称的文件。我有这段代码:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
<?php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename(uploadedfile.jpg);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
这样可行,但文件正在上传,名称为“uploadedfilejpg”,因此没有扩展名。如何解决此问题以添加扩展名?我只想上传jpg文件并覆盖旧文件。
答案 0 :(得分:1)
您在第4行的PHP脚本中出现错误。您已获得basename(uploadedfile.jpg)
,这是一个错误。我假设您打算写basename('uploadedfile.jpg')
,但由于在文件名上使用basename
只返回文件名(感谢@CBroe注意到这一点),您也可以使用'uploadedfile.jpg'
它应该是这样的:
<?php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . 'uploadedfile.jpg';
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
答案 1 :(得分:0)
替换此行:
$uploadfile = $uploaddir . basename('uploadedfile.jpg');
使用:
$uploadfile = $uploaddir . basename('uploadedfile.jpg'). '.jpg';