PHP move_上传的_文件无法工作
我在谷歌搜索过,没有一个答案对我有帮助。我试图在php中使用函数move_uploaded_file,我得到的所有错误如下:PHP move_上传的_文件无法工作,php,ios,Php,Ios,我在谷歌搜索过,没有一个答案对我有帮助。我试图在php中使用函数move_uploaded_file,我得到的所有错误如下: Notice: Undefined index: file in /Applications/XAMPP/xamppfiles/htdocs/photos.php on line 53 Notice: Undefined index: file in /Applications/XAMPP/xamppfiles/htdocs/photos.php on line 57
Notice: Undefined index: file in /Applications/XAMPP/xamppfiles/htdocs/photos.php on line 53
Notice: Undefined index: file in /Applications/XAMPP/xamppfiles/htdocs/photos.php on line 57
当我使用此代码时:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
if (!empty($_POST['firstName']) ) {
$firstname = htmlentities($_POST['firstName']);
echo $firstName;
} elseif (isset($_POST['lastName']) ) {
$lastname = htmlentities($_POST['lastName']);
echo $lastName;
}
if (isset($_POST['userId']) ) {
$userID = htmlentities($_POST['userId']);
echo $userId;
}
$target_dir = "wp-content/uploads/2015/02";
if(!file_exists($target_dir))
{
//chdir($target_dir);
mkdir($target_dir, 0777, true);
}
$target_dir = $target_dir . "/" . basename($_FILES["file"]["name"]);
$moveResult = (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)) ;
if ($moveResult == true) {
echo json_encode([
"Message" => "The file ". basename($_FILES["file"]["name"]). " has been uploaded.",
"Status" => "OK",
"userId" => $_REQUEST["userId"]
]);
} else {
if (isset($_REQUEST['userId']) ) {
$user = $_REQUEST['userId'];
}
echo json_encode([
// echo filename;
"Message" => "Sorry, there was an error uploading your file.",
"Status" => "Error",
//"userId" => $user,
]);
}
?>
我没有收到任何错误,下面是它记录的内容
******* response = <NSHTTPURLResponse: 0x7f8f485a8e60> { URL: http://localhost:490/photos.php } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Length" = 77;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Wed, 01 Jul 2015 16:54:31 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = "Apache/2.4.12 (Unix) OpenSSL/1.0.1m PHP/5.6.8 mod_perl/2.0.8-dev Perl/v5.16.3";
"X-Powered-By" = "PHP/5.6.8";
} }
****** response data = {"Message":"Sorry, there was an error uploading your file.","Status":"Error"}
有什么解决办法吗?
谢谢
您的代码正在运行,请确保以上两点
<head>
<title>A simple example of uploading a file using PHP script</title>
</head>
<body>
<b>Simple example of uploading a file</b><br />
Choose a file to be uploaded<br />
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="500" />
<br />
<input type="submit" value="Upload" />
</form>
</body>
现在在upload_file.php中,确保$target_dir具有正确的文件夹路径来上载文件
enter code here
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
if (!empty($_POST['firstName'])) {
$firstname = htmlentities($_POST['firstName']);
echo $firstName;
} elseif (isset($_POST['lastName'])) {
$lastname = htmlentities($_POST['lastName']);
echo $lastName;
}
if (isset($_POST['userId'])) {
$userID = htmlentities($_POST['userId']);
echo $userId;
}
$target_dir = "C:/xampp/htdocs/test/";
if (!file_exists($target_dir)) {
//chdir($target_dir);
mkdir($target_dir, 0777, true);
}
$target_dir = $target_dir . "/" . basename($_FILES["file"]["name"]);
$moveResult = (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir));
if ($moveResult == true) {
echo json_encode([
"Message" => "The file " . basename($_FILES["file"]["name"]) . " has been uploaded.",
"Status" => "OK",
"userId" => $_REQUEST["userId"]
]);
} else {
if (isset($_REQUEST['userId'])) {
$user = $_REQUEST['userId'];
}
echo json_encode([
// echo filename;
"Message" => "Sorry, there was an error uploading your file.",
"Status" => "Error",
//"userId" => $user,
]);
}
?>
你能提供html表单吗?此通知表示未设置$_FILES['file']。请显示您的html表单代码。您是否在html表单中使用enctype=multipart/form数据?表单标记中是否有类似于:enctype=multipart/form数据的属性?我正在尝试将此服务器用于iOS应用程序,并且正在使用swift。此外,我还尝试检查了$_FILES[file]isset,它返回false,即使我确定它已设置,并且我正在发布该文件!是的,我正在我的应用程序中使用多方/表单数据。我收到以下通知:未定义索引:第50行的/Applications/XAMPP/xamppfiles/htdocs/photos.php中的文件通知:第52行的/Applications/XAMPP/xamppfiles/htdocs/photos.php中的未定义索引:上传文件时出错,状态:错误}在浏览器中注意:未定义索引:第50行的/Applications/XAMPP/xamppfiles/htdocs/photos.php中的文件注意:第52行的/Applications/XAMPP/xamppfiles/htdocs/photos.php中的未定义索引:文件
* First of all make sure that your folder is having permission for read and write
* Secondly input type="file" name="file" size="50
check size of input and change it accordingly.
<head>
<title>A simple example of uploading a file using PHP script</title>
</head>
<body>
<b>Simple example of uploading a file</b><br />
Choose a file to be uploaded<br />
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="500" />
<br />
<input type="submit" value="Upload" />
</form>
</body>
enter code here
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
if (!empty($_POST['firstName'])) {
$firstname = htmlentities($_POST['firstName']);
echo $firstName;
} elseif (isset($_POST['lastName'])) {
$lastname = htmlentities($_POST['lastName']);
echo $lastName;
}
if (isset($_POST['userId'])) {
$userID = htmlentities($_POST['userId']);
echo $userId;
}
$target_dir = "C:/xampp/htdocs/test/";
if (!file_exists($target_dir)) {
//chdir($target_dir);
mkdir($target_dir, 0777, true);
}
$target_dir = $target_dir . "/" . basename($_FILES["file"]["name"]);
$moveResult = (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir));
if ($moveResult == true) {
echo json_encode([
"Message" => "The file " . basename($_FILES["file"]["name"]) . " has been uploaded.",
"Status" => "OK",
"userId" => $_REQUEST["userId"]
]);
} else {
if (isset($_REQUEST['userId'])) {
$user = $_REQUEST['userId'];
}
echo json_encode([
// echo filename;
"Message" => "Sorry, there was an error uploading your file.",
"Status" => "Error",
//"userId" => $user,
]);
}
?>