【问题标题】:How to upload img file in php如何在php中上传图片文件
【发布时间】:2016-12-21 15:03:51
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 请不要用您在答案中发布的代码覆盖您的问题代码。那应该是一个编辑并标记为 EDIT:.... 我试过的”。

标签: php html sql database localhost


【解决方案1】:

您的上传路径($uploaddir = 'localhost'; ) 应该是物理路径而不应该是 url,因此将 localhost 更改为任何路径,例如 ($uploaddir = "uploads/")

完整的演示代码:

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

在“php.ini”文件中,搜索 file_uploads 指令,并将其设置为 On: file_uploads = 开启

然后是你的表单页面

<!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

“upload.php”文件包含上传文件的代码:(是文件上传表单的操作页面)

     <?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

注意:您需要在“upload.php”文件所在的目录中创建一个名为“uploads”的新目录。上传的文件将保存在那里。

【讨论】:

  • 我复制此代码并返回:文件是图像 - image/jpeg。抱歉,上传文件时出错。
  • move_uploaded_file() 函数有问题
  • 第一次制作“uploads”文件夹,您当前的 php 页面放置并检查 Web 服务器是否有权写入“uploads/”目录。只需将“uploads/”文件夹权限设置为 777
  • 哦,对不起。我解释一下:我使用的是 ubuntu,一个用于写入文件夹的系统,例如 /var/www 等(系统文件夹),必须是超级用户,此时我认为这是问题所在。如何更改服务器权限?
  • 我更改了一些代码,以证明它是目录可写 (is_writable) 问题,或者您提供了错误的路径,该路径未在您的系统中退出
【解决方案2】:

$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>";

【讨论】:

  • 我检查了 php.ini 文件,它是:file_uploads = On。然后我以这种方式更改我的代码:
  • @DiegoZucca 正如我在回答中所写,$uploaddir = 'localhost'; localhost 保留给数据库语法,不被视为文件夹。阅读手册和我从中提取的示例,并确保可以写入它的文件夹。
【解决方案3】:

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

我还删除了您在div 上添加的display:none; 样式以使其可用,因为您没有提供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 / 文件有效,成功 已上传。

【讨论】:

  • 所以你认为问题是因为 div 没有显示所以浏览器没有获取 thid div 获取的任何 img 数据?
  • 没有。请拿走我的文件,直接试一试,不要修改任何东西,告诉我它是否有效,因为如果它没有,那么这意味着你有一个需要更改的服务器配置。告诉我,谢谢。
  • 你试过了吗?这是您自己的脚本,只需进行很少的修改即可使其正常工作。你应该试试看!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-02
  • 2021-07-15
  • 1970-01-01
  • 2017-03-26
  • 2018-07-19
  • 1970-01-01
相关资源
最近更新 更多