在 PHP 中上传文件
我正在尝试使用 PHP 上传文件。
我的文件夹结构如下:
我的根 htdocs 文件夹中有一个名为 schemas
的目录。在这个文件夹中,我想为每个用户创建一个新文件夹;在此文件夹中,我将存储特定于用户的文件。
目前,我的代码不完整(不检查文件夹/文件是否已经存在,并且可能会出错),但我只是想让基础知识发挥作用。
<?php
if ($_FILES ["bestand"] ["error"] > 0) {
//for error messages: see http://php.net/manual/en/features.fileupload.errors.php
switch ($_FILES ["bestand"] ["error"]) {
case 1 :
$msg = "U mag maximaal 2MB opladen.";
break;
default :
$msg = "Sorry, uw upload kon niet worden verwerkt.";
}
} else {
//check MIME TYPE - http://php.net/manual/en/function.finfo-open.php
$allowedtypes = array ("application/pdf" );
$filename = $_FILES ["bestand"] ["tmp_name"];
$finfo = new finfo ( FILEINFO_MIME_TYPE );
$fileinfo = $finfo->file ( $filename );
if (in_array ( $fileinfo, $allowedtypes )) {
//move uploaded file
$dir = "schemas";
chdir("schemas");
$user_folder = str_replace(" ", "", $_POST['schema']);
mkdir( $user_folder, 0777);
// echo getcwd();
// closedir($open);
$folder = "/schemas/" . $user_folder . "/" . $_FILES ["bestand"] ["name"];
echo $folder;
echo $user_folder;
if (move_uploaded_file ( $_FILES ["bestand"] ["tmp_name"], $folder )) {
$msg = "Uw schema is succesvol geupload!";
} else {
$msg = "Upload mislukt.";
}
} else {
$msg = "U kan enkel een pdf uploaden.";
}
}
echo $msg . "<br />";
?>
当我尝试此操作时,我收到以下警告:
Warning: move_uploaded_file(/schemas/loesp/loopschema-0-tot-5-kilometer.pdf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Sportjefit2/uploadenfile.php on line 30
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/MAMP/tmp/php/phpMsxxsu' to '/schemas/loesp/loopschema-0-tot-5-kilometer.pdf' in /Applications/MAMP/htdocs/Sportjefit2/uploadenfile.php on line 30
脚本确实创建了文件夹(在本例中为loesp
),但似乎没有上传或移动实际文件。我很困惑。
I'm trying to upload a file using PHP.
My folder structure is the following:
I have a directory called schemas
in my root htdocs folder. Inside this folder, I want to create a new folder for every user; in this folder I'll be storing user-specific files.
For now, my code is incomplete (doesn't check whether or not the folder/file already exists and might error on this), but I'm just trying to get the basics to work.
<?php
if ($_FILES ["bestand"] ["error"] > 0) {
//for error messages: see http://php.net/manual/en/features.fileupload.errors.php
switch ($_FILES ["bestand"] ["error"]) {
case 1 :
$msg = "U mag maximaal 2MB opladen.";
break;
default :
$msg = "Sorry, uw upload kon niet worden verwerkt.";
}
} else {
//check MIME TYPE - http://php.net/manual/en/function.finfo-open.php
$allowedtypes = array ("application/pdf" );
$filename = $_FILES ["bestand"] ["tmp_name"];
$finfo = new finfo ( FILEINFO_MIME_TYPE );
$fileinfo = $finfo->file ( $filename );
if (in_array ( $fileinfo, $allowedtypes )) {
//move uploaded file
$dir = "schemas";
chdir("schemas");
$user_folder = str_replace(" ", "", $_POST['schema']);
mkdir( $user_folder, 0777);
// echo getcwd();
// closedir($open);
$folder = "/schemas/" . $user_folder . "/" . $_FILES ["bestand"] ["name"];
echo $folder;
echo $user_folder;
if (move_uploaded_file ( $_FILES ["bestand"] ["tmp_name"], $folder )) {
$msg = "Uw schema is succesvol geupload!";
} else {
$msg = "Upload mislukt.";
}
} else {
$msg = "U kan enkel een pdf uploaden.";
}
}
echo $msg . "<br />";
?>
When I try this, I get the following warnings:
Warning: move_uploaded_file(/schemas/loesp/loopschema-0-tot-5-kilometer.pdf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Sportjefit2/uploadenfile.php on line 30
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/MAMP/tmp/php/phpMsxxsu' to '/schemas/loesp/loopschema-0-tot-5-kilometer.pdf' in /Applications/MAMP/htdocs/Sportjefit2/uploadenfile.php on line 30
The script does create the folder (in this case loesp
) but does not seem to upload or move the actual file. I'm stumped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您使用的是绝对路径,该路径从整个计算机的根目录开始。您应该使用相对路径,并且应该从当前工作目录开始。
您已经位于
schemas
文件夹中(在调用 chdir 之后),因此 $folder 应设置为:The problem is that you're using an absolute path, which starts in the root of the entire machine. You should use a relative path, and it should start from the current working directory.
You already are inside the
schemas
folder (after the call to chdir), so $folder should be set to this: