我刚开始使用本地主机中的脚本上传文件,
但每次我想上传文件时都会出错:
警告:chmod() [function.chmod]:第 5 行的 /Applications/XAMPP/xamppfiles/htdocs/admin/upload.php 中没有这样的文件或目录
警告:chmod() [function.chmod]:第 6 行的 /Applications/XAMPP/xamppfiles/htdocs/admin/upload.php 中没有这样的文件或目录
我猜是权限和路径的问题,但我不知道如何解决,我的代码:
<?php
define("UPLOAD_DIR",realpath(dirname(__FILE__)).'/uploads' );
// set proper permissions on the new file
chmod(realpath(dirname(__FILE__)).'/uploads', 0777);
chmod(realpath(dirname(__FILE__)).'/uploads/'.$name, 0777);
if (!empty($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
echo "<p>An error occurred.</p>";
exit;
}
// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
UPLOAD_DIR . $name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
}
?>