15

好吧,我在这方面投入了很多时间。我是 PHP 编程的新手并试图掌握基础知识,但截至昨晚我有点迷失,我能够获得一个 PHP 表单来上传基本数据,如名称地址和我的(MySQL)服务器。

但是今天我说让我们做下一步,这将是一个图像到服务器。我已经在 YouTube 上观看了 3 个视频,大概 100 次只是重新复制代码并以多种不同的方式尝试它。

http://www.youtube.com/watch?v=CxY3FR9doHI
http://www.youtube.com/watch?v=vFZfJZ_WNC4&feature=relmfu

仍然无法得到它。

但长话短说:我有一个连接到服务器的 config.php 文件,这是我在上传表单页面上运行的代码:

<html>
  <head>
    <title>Upload an image</title>
  </head>
<body>
  <form action="UploadContent.php" method="POST" enctype="multipart/form-data">
  File:
    <input type="file" name="image"> <input type="submit" value="Upload">
  </form>
<?php

// connect to database
include"config.php";

// file properties
$file = $_FILES['image']['tmp_name'];

if (!isset($file))
  echo "Please select a profile pic";
else
{
  $image = addslashes(file_get_content($_FILES['image']['tmp_name']));
  $image_name = addslashes($FILES['image']['name']);
  $image_size = getimagesize($_FILES['image']['tmp_name']);

  if ($image_size==FALSE)
    echo "That isn't a image.";
  else
  {
    $insert = mysql_query("INSERT INTO content VALUES ('','','','','','','','','','$image_name','$image',)");
  }
}
?>
  </body>
</html>

插入行上所有的原因'', '', '', ''是因为我在第 10 个字段中有名称,在第 11 个字段中有图像 blob,所有导致这些的都是名字、姓氏和类似的随机内容。我怎样才能解决这个问题?它返回错误:

致命错误:在第 22 行的 /home/content/34/9587634/html/WEBPAGE/UploadContent.php 中调用未定义函数 file_get_content()

我不知道该怎么办。

4

9 回答 9

10

该代码忽略了调用函数 move_uploaded_file() ,该函数将检查指示的文件是否对上传有效。

您可能希望在以下位置查看一个简单的示例:

http://www.w3schools.com/php/php_file_upload.asp

于 2012-11-20T03:11:58.757 回答
5

您需要添加两个新文件,一个是 index.html,复制并粘贴以下代码,另一个是 imageup.php,它将上传您的图像

 <form action="imageup.php" method="post" enctype="multipart/form-data">
 <input type="file" name="banner" >
 <input type="submit" value="submit">
 </form>

 imageup.php
 <?php
 $banner=$_FILES['banner']['name']; 
 $expbanner=explode('.',$banner);
 $bannerexptype=$expbanner[1];
 date_default_timezone_set('Australia/Melbourne');
 $date = date('m/d/Yh:i:sa', time());
 $rand=rand(10000,99999);
 $encname=$date.$rand;
 $bannername=md5($encname).'.'.$bannerexptype;
 $bannerpath="uploads/banners/".$bannername;
 move_uploaded_file($_FILES["banner"]["tmp_name"],$bannerpath);
 ?>

上面的代码将使用加密名称上传您的图像

于 2015-12-10T10:57:08.987 回答
4

file_get_content()将代码中的函数更改为file_get_contents(). 您在函数名称的末尾缺少“s”。这就是为什么它给出未定义的函数错误。

文件获取内容()

$image在行中归档后删除最后一个不必要的逗号

"INSERT INTO content VALUES         ('','','','','','','','','','$image_name','$image',)
于 2012-11-20T03:00:12.523 回答
2

我建议您将图像保存在服务器中,然后将 URL 保存在 MYSQL 数据库中。

首先,你应该对你的图像做更多的验证,在未经验证的文件可能导致巨大的安全风险之前。

  1. 检查图像

    if (empty($_FILES['image']))
      throw new Exception('Image file is missing');
    
  2. 将图像保存在变量中

    $image = $_FILES['image'];
    
  3. 检查上传时间错误

    if ($image['error'] !== 0) {
       if ($image['error'] === 1) 
          throw new Exception('Max upload size exceeded');
    
       throw new Exception('Image uploading error: INI Error');
    }
    
  4. 检查上传的文件是否存在于服务器中

    if (!file_exists($image['tmp_name']))
        throw new Exception('Image file is missing in the server');
    
  5. 验证文件大小(根据需要更改)

     $maxFileSize = 2 * 10e6; // = 2 000 000 bytes = 2MB
        if ($image['size'] > $maxFileSize)
            throw new Exception('Max size limit exceeded'); 
    
  6. 验证图像(检查文件是否为图像)

     $imageData = getimagesize($image['tmp_name']);
         if (!$imageData) 
         throw new Exception('Invalid image');
    
  7. 验证图像 mime 类型(根据您的需要执行此操作)

     $mimeType = $imageData['mime'];
     $allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];
     if (!in_array($mimeType, $allowedMimeTypes)) 
        throw new Exception('Only JPEG, PNG and GIFs are allowed');
    

这可能会帮助您使用 PHP 创建安全的图像上传脚本。

代码来源:https ://developer.hyvor.com/php/image-upload-ajax-php-mysql

此外,我建议您使用MYSQLI 准备好的语句进行查询以提高安全性。

谢谢你。

于 2018-10-05T10:29:05.290 回答
1

同一页面上的简单 PHP文件/图像上传代码。

<form action="" method="post" enctype="multipart/form-data">
  <table border="1px">
    <tr><td><input type="file" name="image" ></td></tr>
    <tr><td> <input type="submit" value="upload" name="btn"></td></tr>
  </table>
</form>

 <?php
   if(isset($_POST['btn'])){
     $image=$_FILES['image']['name']; 
     $imageArr=explode('.',$image); //first index is file name and second index file type
     $rand=rand(10000,99999);
     $newImageName=$imageArr[0].$rand.'.'.$imageArr[1];
     $uploadPath="uploads/".$newImageName;
     $isUploaded=move_uploaded_file($_FILES["image"]["tmp_name"],$uploadPath);
     if($isUploaded)
       echo 'successfully file uploaded';
     else
       echo 'something went wrong'; 
   }

 ?>
于 2019-05-01T17:03:10.873 回答
1

这是一个基本示例,说明如何将具有某些限制(如下所列)的图像文件上传到服务器。

  • 图像的存在。
  • 图片扩展验证
  • 检查图像大小。

    <?php
       $newfilename = "newfilename";
    
       if(isset($_FILES['image'])){
          $errors= array();
          $file_name = $_FILES['image']['name'];
          $file_size =$_FILES['image']['size'];
          $file_tmp =$_FILES['image']['tmp_name'];
          $file_type=$_FILES['image']['type'];
          $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
    
          $expensions= array("jpeg","jpg","png");
          if(file_exists($file_name)) {
            echo "Sorry, file already exists.";
            }
          if(in_array($file_ext,$expensions)=== false){
             $errors[]="extension not allowed, please choose a JPEG or PNG file.";
          }
    
          if($file_size > 2097152){
             $errors[]='File size must be excately 2 MB';
          }
    
          if(empty($errors)==true){
            move_uploaded_file($file_tmp,"images/".$newfilename.".".$file_ext);
            echo "Success";
            echo "<script>window.close();</script>";
    
          }
    
          else{
             print_r($errors);
          }
       }
    ?>
    <html>
       <body>
    
          <form action="" method="POST" enctype="multipart/form-data">
             <input type="file" name="image" />
             <input type="submit"/>
          </form>
    
       </body>
    </html>
    

    Credit to this page.

于 2019-12-04T05:00:14.677 回答
0
<?php

$filename=$_FILES['file']['name'];
$filetype=$_FILES['file']['type'];
if($filetype=='image/jpeg' or $filetype=='image/png' or $filetype=='image/gif')
{
move_uploaded_file($_FILES['file']['tmp_name'],'dir_name/'.$filename);
$filepath="dir_name`enter code here`/".$filename;
}

?> 
于 2018-06-07T20:43:19.710 回答
0
  <?php 
 $target_dir = "images/";
    echo $target_file = $target_dir . basename($_FILES["image"]["name"]);
    $post_tmp_img = $_FILES["image"]["tmp_name"];
    $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
    $post_imag = $_FILES["image"]["name"];
        move_uploaded_file($post_tmp_img,"../images/$post_imag");
 ?>
于 2018-08-13T23:46:20.680 回答
0

这段代码很容易通过 php 上传文件。在这段代码中,我在同一页面中执行上传任务,这意味着我们的 html 和 php 代码都驻留在同一个文件中。此代码生成图像名称的新名称。

首先看html代码

<form action="index.php" method="post" enctype="multipart/form-data">
 <input type="file" name="banner_image" >
 <input type="submit" value="submit">
 </form>

现在看php代码

<?php
$image_name=$_FILES['banner_image']['name'];
       $temp = explode(".", $image_name);
        $newfilename = round(microtime(true)) . '.' . end($temp);
       $imagepath="uploads/".$newfilename;
       move_uploaded_file($_FILES["banner_image"]["tmp_name"],$imagepath);
?>
于 2019-03-03T14:30:27.783 回答