[英]PHP Problems (file uploading)
I have been working on a website for some time now, and the code I used here worked about a month ago, then I tried putting the file in a subfolder, and redirecting the move_uploaded_file function, but it doesn't seem to be working. 我已经在网站上工作了一段时间,我在这里使用的代码大约一个月前就起作用了,然后我尝试将文件放在子文件夹中,并重定向move_uploaded_file函数,但它似乎没有用。 To be specific, both the:
具体来说,两个都:
if((!empty($_FILES["uploaded_letter"])) && ($_FILES['uploaded_letter']['error'] == 0))
and 和
if((!empty($_FILES["uploaded_list"])) && ($_FILES['uploaded_list']['error'] == 0))
statements are returning false. 语句返回false。 If anyone could please help with that, that would be awesome!
如果有人可以帮助您,那就太好了! Thanks!
谢谢!
<html>
<head>
<title>VAA Chapter 11</title>
<!--favicon-->
<link rel="shortcut icon" href="favicon.ico" />
<!--global CSS-->
<link href="vaa.css" rel="stylesheet" type="text/css">
<!--local CSS-->
<style type="text/css">
</style>
</head>
<body>
<?php
function testPassword(){
$password = $_POST['password'];
if($password == 'You aint guessin this'){
echo 'welcome!!! <br/>
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> <br/>
add newsletter: <input name="uploaded_letter" type="file" /><br/><br/>
add list: <input name="uploaded_list" type="file" /><br/><br/>
<input type="submit" name="load" value="Upload" />
</form>';
}
else{
echo "you have no power here";
}
}
function upload(){
if((!empty($_FILES["uploaded_letter"])) && ($_FILES['uploaded_letter']['error'] == 0)) {
$filename = basename($_FILES['uploaded_letter']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "pdf") && ($_FILES["uploaded_letter"]["size"] < 35000000)) {
$newname = dirname(__FILE__,1).'/newsletters/'.$filename;
move_uploaded_file($_FILES['uploaded_letter']['tmp_name'],$newname);
}
else {
echo "Error: file is too large";
}
}
else {
echo "Error: No file uploaded";
}
if((!empty($_FILES["uploaded_list"])) && ($_FILES['uploaded_list']['error'] == 0)) {
$filename = basename($_FILES['uploaded_list']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "txt") && ($_FILES["uploaded_file"]["size"] < 35000)) {
$newname = dirname(__FILE__,1).'/'.$filename;
move_uploaded_file($_FILES['uploaded_list']['tmp_name'],$newname);
}
else {
echo "Error: file is too large";
}
}
else {
echo "Error: No file uploaded";
}
}
function testUpload(){
if(isset($_POST['load'])){
upload();
}
}
?>
<form method="post">
<input type="text" name="password" />
<input type="submit" value="OK" />
</form>
<?php testPassword(); testUpload(); ?>
</body>
</html>
最好有多个文件要上传以使用数组命名文件,例如add newsletter: <input name="uploaded_letter[]" type="file" />
add list: <input name="uploaded_letter[]" type="file" />
则可以在目标页面中使用foreach
Use this, 用这个,
if((!empty($_FILES["uploaded_letter"]["name"]))
{
// Your Procedures
}
if((!empty($_FILES["uploaded_list"]["name"]))
{
// Your Procedures
}
I think you should use if and else if conditions properly in upload() like: 我认为您应该在上载条件中使用if和else if条件,例如:
function upload(){
if((!empty($_FILES["uploaded_letter"])) && ($_FILES['uploaded_letter']['error'] == 0)) {
//your code......
}
//missing elseif statement instead of if
elseif ((!empty($_FILES["uploaded_list"])) && ($_FILES['uploaded_list']['error'] == 0)) {
//your code....
else {
echo "Error: file is too large";
}
}
else {
echo "Error: No file uploaded";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.