douluan4644 2015-10-28 18:59
浏览 51
已采纳

基本的php5文件上传问题

I'm currently coding the most basic file upload to go to our server from an input type="file" attribute. This is my HTML:

<form enctype="multipart/form-data" action="register-complete.php" method="post">
        <h5>Register Now</h5>
        <input type="text" class="form-control" name="Username" placeholder="Login Name"/><br />
        <input type="text" class="form-control" name="Displayname" placeholder="Display Name"/><br />
        <input type="text" class="form-control" name="Email" placeholder="Email" /><br />
        <input type="radio" name="Paypal" value="1" /> This is my Paypal email.<br />
        <input type="radio" name="Paypal" value="0" /> I do not want payment. I wish to preserve anonymity.<br /><br />
        Avatar Picture: <br /><input type="file" name="AvatarPicture" id="AvatarPicture" />*500kb max file size.<br />*Accepted filetypes: .jpg, .png<br /><br />
        <input type="text" class="form-control" name="Description" placeholder="Account Description"/><br />
        <input type="password" class="form-control" name="Password" placeholder="Password"/><br />
        <input type="password" class="form-control" name="PasswordConfirm" placeholder="Confirm Password"/><br />
        <p class="text-center"><input type="submit" class="btn btn-primary" value="Register" name="submit" /></p>
    </form>

Basically I'm just concerned with the AvatarPicture input, and just get it to upload a file to my server. Here is the PHP code I have to do that.

$username = $_REQUEST["Username"];
$displayname = $_REQUEST["Displayname"];
$email = $_REQUEST["Email"];
$paypal = $_REQUEST["Paypal"];

$target_dir = "images/avipictures/";
$target_file = $target_dir . basename($_FILES["AvatarPicture"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["AvatarPicture"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}

// Check file size
if ($_FILES["AvatarPicture"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
    echo "Sorry, only JPG, JPEG, PNG files are allowed.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["AvatarPicture"]["tmp_name"],$target_file)) {
        echo "The file has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

This is literally the same exact thing as the w3schools code to the tee. However I am receiving an internal server 500 error unless I change the "tmp_name" in the move_uploaded_file() to "name", which just leads to the else statement. I've been messing around with this all day and I have just been tearing my hair out at just how simple this bit of code should be but doesnt seem to be at all. Any ideas? (Also, the file_uploads is set to on and the default largest file size is set to 50mb.)

  • 写回答

1条回答 默认 最新

  • dongxiong2000 2015-10-28 19:39
    关注

    I can't tell what the problem is without a decent log file, but it could be that the directory you're trying to write to doesn't exist or you don't have permission to write to it. Are you sure that the images/avipictures/ directory that you're trying to write to exists, and that the webserver has permission to write to it? If not, you'll need to create the directory and set the permissions on it such that the webserver can write to it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线