2012-02-16 85 views
1

好吧,我正在尝试创建一个网页,将照片上传到Apache htdocs中的文件夹。并且...呃...它不工作...我一直在寻找教程,并且整个事情都需要在PHP中完成(一些HTML也允许[不使用闪光])。PHP将文件上传到文件夹(在apache中)

这里是我....

<p>Browse For a File on your computer to upload it!</p> 
<form enctype="multipart/form-data" action="upload_photos.php" method="POST"> 
<input type="hidden" name="MAX_FILE_SIZE" value="250000" /> 
Choose a file to upload: <input name="uploadedfile" type="file" /><br /> 
<input type="submit" value="Upload File" /> 
</form> 


     <?PHP  

    if ($uploadedfile_size >250000) 
    {$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>"; 
    $file_upload="false";} 

    else{ 

    if (!($uploadedfile_type<>"image/jpeg" OR $userfile_type<>"image/tiff" OR $userfile_type<>"image/png")) 
    {$msg=$msg."Your uploaded file must be of JPG, PNG, or tiff. Other file types are not allowed<BR>"; 
    $file_upload="false";} 

    } 
     ?> 

     <!-- 
    •enctype="multipart/form-data" - Necessary for our PHP file to function properly. 
    •action="upload_photos.php" - The name of our PHP page that was created. 
    •method="POST" - Informs the browser that we want to send information to the server using POST. 
    •input type="hidden" name="MA... - Sets the maximum allowable file size, in bytes, that can be uploaded. This safety mechanism is easily bypassed and we will show a solid backup solution in PHP. We have set the max file size to 100KB in this example. 
    •input name="uploadedfile" - uploadedfile is how we will access the file in our PHP script. 

     --> 

</form> 

    </label> 
</form> 

现在,它不会越过这个点,但是一旦进入到其他页面(upload_photos.php)它说:“有一个上传文件时出错, 请再试一次!”

<?php 

/* 
When the uploader.php file is executed, the uploaded file exists in a temporary storage area on the server. If the file is not moved to a different location it will be destroyed! To save our precious file we are going to need to make use of the $_FILES associative array. 

The $_FILES array is where PHP stores all the information about files. There are two elements of this array that we will need to understand for this example. 
•uploadedfile - uploadedfile is the reference we assigned in our HTML form. We will need this to tell the $_FILES array which file we want to play around with. 
•$_FILES['uploadedfile']['name'] - name contains the original path of the user uploaded file. 
•$_FILES['uploadedfile']['tmp_name'] - tmp_name contains the path to the temporary file that resides on the server. The file should exist on the server in a temporary directory with a temporary name. 

*/ 

// Where the file is going to be placed 
$target_path = "uploads/"; 

/* Add the original filename to our target path. 
Result is "uploads/filename.extension" */ 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 


/* 
Now all we have to do is call the move_uploaded_file function and let PHP do its magic. The move_uploaded_file function needs to know 1) The path of the temporary file (check!) 2) The path where it is to be moved to (check!). 
*/ 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 


/* 

If the upload is successful, then you will see the text "The file filename has been uploaded". This is because move_uploaded_file returns true if the file was moved, and false if it had a problem. 

If there was a problem then the error message "There was an error uploading the file, please try again!" would be displayed. 

*/ 

?> 

请帮助我,我一直在逐行浏览它,我不知道它为什么这样做。这可能是一个语法错误或什么的。我是全新的PHP,所以我不怀疑它。无论如何,提前感谢。

+0

将$ target_path更改为完整的服务器路径。 – 2012-02-16 19:43:43

+0

你有什么相关的Apache设置? Apache也可以对文件上传的类型和大小施加限制。你的Apache错误日志说什么? – 2012-02-16 19:43:44

+0

“我是PHP新品”---这是学习如何调试**代码的好机会。没有人会为你做这件事,那是你作为程序员的日常工作 – zerkms 2012-02-16 19:44:38

回答

0

删除一个PHPinfo()文件下来,并检查什么的Apache有在核心的upload_max_filesize。这可能设置得太低。

如果您忽略它,请检查目标目录上的权限。在写入目标目录之前,请确保目标目录设置为775或目标目录为chmod

0

变化:

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 

到:

然后让我们知道的var_dump返回

+0

我会稍微做一点。现在我的笔记本电脑正在进行更新(当然),我会尽快完成更新。 (不久前我刚收到笔记本电脑,所以有很多Windows更新) – 2012-02-16 19:51:04

+0

没问题,var_dump返回的唯一东西是“bool(false)”。我也尝试将$ target_path更改为完整路径...我现在将尝试chmod权限 – 2012-02-16 21:00:58