php 如何使用php将文件上传到特定文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29007074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:12:03  来源:igfitidea点击:

How to upload a file to a specific folder using php?

php

提问by Prerna

My code is as follows..Here when I upload a file named Koala.jpg,then the warning is shown--Warning: copy(Koala.jpg): failed to open stream: No such file or directory in E:\xampp\htdocs\Forum\upload.php on line 4 Could not copy file!..Please solve the problem urgently..

我的代码如下..这里当我上传一个名为Koala.jpg的文件时,显示警告--Warning: copy(Koala.jpg): failed to open stream: No such file or directory in E:\xampp\ htdocs\Forum\upload.php on line 4 无法复制文件!..请紧急解决问题..

Select a file to upload: <br />
            <form action="upload.php" method="post" enctype="multipart/form-data">
            <input type="file" name="file" size="50" />
            <br />
            <input type="submit" value="Upload File" />
            </form>

***upload.php***

<?php
if( $_FILES['file']['name'] != "" )
{
   copy( $_FILES['file']['name'], "/uploads" ) or 
           die( "Could not copy file!");
}
else
{
    die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name'];  ?>
<li>File size: <?php echo $_FILES['file']['size'];  ?> bytes
<li>File type: <?php echo $_FILES['file']['type'];  ?>
</ul>
</body>
</html>

回答by Mihir Bhatt

Replace copy function by following

通过以下替换复制功能

$destFile = "/root/mysite/upload_files/".$_FILES['file']['name'];
move_uploaded_file( $_FILES['file']['tmp_name'], $destFile );

Note the location is a filesystem destination nota URL. Also the filesystem path is relative to the highest root rather than the website home URL.

请注意,位置是文件系统目标,而不是URL。此外,文件系统路径相对于最高根目录而不是网站主页 URL。

回答by starkeen

Try this

尝试这个

<?php
if( $_FILES['file']['name'] != "" ) {
    $path=$_FILES['file']['name'];
    $pathto="/uploads/".$path;
    move_uploaded_file( $_FILES['file']['tmp_name'],$pathto) or die( "Could not copy file!");
}
else {
    die("No file specified!");
}
?>

Source:

来源

http://php.net/manual/en/features.file-upload.post-method.php

http://php.net/manual/en/features.file-upload.post-method.php

http://www.tizag.com/htmlT/htmlupload.php

http://www.tizag.com/htmlT/htmlupload.php