我试图在php中添加一个文件上传脚本到我正在设计的网站。我使用了一个在线示例(我知道它不安全,我计划让它安全,我只想让基本功能首先运行)。
基本上发生的事情是,当我点击“提交”按钮时,页面会暂停并说“加载xyz服务器..”,永远不会进入post action php页面!这非常令人沮丧,我不明白为什么它不起作用!
代码如下,我已经在2台不同的服务器上尝试了这一结果。如果有人能让我知道我可能做错了什么,我会非常感激。
Choose a file to upload:
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
($_FILES["uploaded_file"]["size"] < 350000)) {
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'/upload/'.$filename;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
echo "It's done! The file has been saved as: ".$newname;
} else {
echo "Error: A problem occurred during file upload!";
}
} else {
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
} else {
echo "Error: Only .jpg images under 350Kb are accepted for upload";
}
} else {
echo "Error: No file uploaded";
}
?>非常感谢您的时间,我已经搜索了几个小时来解决这个问题,但没有运气!