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

时间:2015-03-12 10:19:31

标签: php

我的代码如下。当我上传名为Koala.jpg的文件时,会显示警告 - 警告:复制(Koala.jpg):无法打开流:E中没有此类文件或目录:第4行\ xampp \ htdocs \ Forum \ upload.php 无法复制文件!..请紧急解决问题..

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>

2 个答案:

答案 0 :(得分:1)

按照

替换复制功能
$destFile = "/root/mysite/upload_files/".$_FILES['file']['name'];
move_uploaded_file( $_FILES['file']['tmp_name'], $destFile );

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

答案 1 :(得分:1)

试试这个

<?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!");
}
?>

来源

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

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