如何在php中上传img文件

时间:2016-12-21 15:03:52

标签: php html sql database localhost

我尝试使用PHP上传服务器中的文件,但是我遇到了一些问题。我找到了这个指南:http://www.sumedh.info/articles/store-upload-image-postgres-php-2.html。 我的HTML是:

<form action="img_user.php" method="POST" enctype="multipart/form-data" >
  <button id="buttonImgProf" class="btn" type="button" onclick="caricaImgProf()">Inserisci un immagine</button>

  <div id="imgProfLoader"  class="postContent" style="display:none;">
    Name : <input type="text" name="name" size="25" length="25" value="">
    <input type="file" name="userfile"></input>
    <button class="btn" type="submit">Carica immagine</button>
  </div>
</form>

(部件不显示,因为我使用javascript)。 php代码是:

$uploaddir = 'localhost'; //i have try lots of dir, maybe the error is here?

    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
    $name = $_POST['name'];
    echo $_FILES['userfile']['tmp_name'];
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
    {    echo "File is valid, and was successfully uploaded.\n";
    }
    else   {   echo "File not uploaded";   }

输出为File not uploaded

3 个答案:

答案 0 :(得分:1)

$uploaddir = 'localhost'; - localhost用于数据库连接,而不是文件夹上传指令;使用可写的文件夹。

根据手册http://php.net/manual/en/features.file-upload.post-method.php

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

答案 1 :(得分:0)

你的上传路径($ uploaddir =&#39; localhost&#39;;)应该是物理路径,不应该是url所以将localhost更改为任何路径,如($ uploaddir =&#34; uploads /&#34;)

完整的演示代码:

首先,确保将PHP配置为允许文件上传。

在你的&#34; php.ini&#34; file,搜索file_uploads指令,并将其设置为On: file_uploads = On

然后是您的表单页面

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>



Make sure that the form uses method="post"
The form also needs the following attribute: enctype="multipart/form-data". It specifies which content-type to use when submitting the form

&#34; upload.php&#34; file包含上传文件的代码:(这是文件上传表单的操作页面)

     <?php
        $target_dir = "uploads/";
    if (is_dir($upload_dir) && is_writable($upload_dir)) {
    // you can write anything in this folder 
} else {
    die('Upload directory is not writable, or does not exist.');
}
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["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["fileToUpload"]["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 $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["fileToUpload"]["tmp_name"], $target_file)) {
                echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
        }
        ?>

PHP脚本解释:

$target_dir = "uploads/" - specifies the directory where the file is going to be placed
$target_file specifies the path of the file to be uploaded
$uploadOk=1 is not used yet (will be used later)
$imageFileType holds the file extension of the file
Next, check if the image file is an actual image or a fake image

注意:您需要创建一个名为&#34; uploads&#34;的新目录。在&#34; upload.php&#34;的目录中文件驻留。上传的文件将保存在那里。

答案 2 :(得分:0)

我将您的HTML和PHP代码放在一个文件中并用“非空”$_FILES包装您的PHP代码,它似乎工作正常。这是完整的文件。

由于您未提供display:none;功能的内容,因此我还删除了您在div上放置的caricaImgProf()样式以使其可用。哦,$uploaddir不能是localhost这样的域,它必须是一个有效的目录,所以我删除了你的localhost引用,以便文件上传到脚本所在的位置(同一目录)你当然不应该在生产服务器上做什么,但这不是主题:P

此单个文件应命名为img_user.php:

<?php

if(!empty($_FILES)){
    $uploadfile = basename($_FILES['userfile']['name']);
    $name = $_POST['name'];
    echo $_FILES['userfile']['tmp_name'];
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
    {    echo " / File is valid, and was successfully uploaded.\n";
    }
    else   {   echo "File not uploaded";   }
    exit();
}

?><html>
<body>
<form action="img_user.php" method="POST" enctype="multipart/form-data">
    <button id="buttonImgProf" class="btn" type="button" onclick="caricaImgProf()">Inserisci un immagine</button>
    <div id="imgProfLoader" class="postContent">
        Name : <input type="text" name="name" size="25" length="25" value="">
        <input type="file" name="userfile" />
        <button class="btn" type="submit">Carica immagine</button>
    </div>
</form>
</body>
</html>

当我提交图片文件时,它会在上传之前正确地将文件保存在与原始文件名相同的文件夹中的PHP脚本旁边输出:

  

E:\ wamp64 \ tmp \ php38E6.tmp /文件有效,并且已成功完成   上传。