简体   繁体   English

使用PHP和HTML上传文件

[英]Uploading files with PHP & HTML

I'm having trouble with setting up the file upload to my php script. 我在设置文件上传到我的php脚本时遇到了麻烦。

Within the upload itself, I have it directed towards the path I want, but I'm not sure if I have the rest of the script syntactically right. 在上载本身中,我已将其定向到所需的路径,但是我不确定语法上其余的脚本是否正确。

EDIT: I have resolved my situation with the file not properly uploading. 编辑:我已经解决了我的情况,文件未正确上传。 I have edited the blocks of code to what I have currently working. 我已经将代码块编辑为当前正在工作的代码块。 If needed, please refer to the pre-edited version to see changes :). 如果需要,请参考预编辑版本以查看更改:)。

Cheers. 干杯。

$tardir= "C:/xampp/htdocs/uploaddir/$targetname";
$uploadOk=1;

if(isset($_FILES["uploadFile"])){
    $tardir= $tardir . "/" . basename($_FILES["uploadFile"]["name"]);
    if(move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $tardir)){ 
        echo "The file: ". basename($_FILES["uploadFile"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}

The $targetname is set to whatever the user inputs in the field. $targetname设置为用户在字段中输入的任何内容。 In this case, I have it as their email. 在这种情况下,我将其作为他们的电子邮件。

$targetname = filter_input(INPUT_POST, 'email');

This is what the <html> for the file upload looks like: 文件上传的<html>如下所示:

<form action= "userlogin.php" method= "post" enctype= "multipart/form-data">
        <p><strong>Please choose a file to upload: <input type="file" name="uploadFile"><br></p>
           <input type="submit" value="Upload File">

It's hard to guess all your code, as it seems you only have a snippet here, but I'm gonna take a wild guess. 很难猜测所有代码,因为您似乎在这里只有一小段代码,但我会做出一个疯狂的猜测。 If target name comes from a text input like you say and you have it set to e-mail, are you leaving off a slash here? 如果目标名称来自您所说的文本输入,并且已将其设置为电子邮件,那么您是否在此处留下斜线?

$tardir= "C:/xampp/htdocs/uploaddir/$targetname";

This would be like: 就像:

C:/xampp/htdocs/uploaddir/me@gmail.com

This runs: 运行:

$tardir= $tardir . basename($_FILES["uploadFile"]["name"]);

then: 然后:

 C:/xampp/htdocs/uploaddir/me@gmail.comfilename

shouldn't it be: 不应该是:

C:/xampp/htdocs/uploaddir/me@gmail.com/filename

you can also comment out all your code here and var_dump() your file array, and echo out your paths before you try using them to see what data is held in each of your containers. 您还可以在此处注释掉所有代码以及文件数组var_dump(),并在尝试使用它们查看每个容器中保存的数据之前回显路径。 If I get a bug like this I dump everything and echo every string until I find it.. 如果遇到这样的错误,我将丢弃所有内容并回显每个字符串,直到找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM