Problem with php uploading images that cannot be displayed

卡哇伊
Release: 2023-04-09 06:52:01
Original
2963 people have browsed it

The general method for uploading images to the database and then displaying them is to write a dedicated PHP page, obtain the image ID through the GET or POST method, query the database, output the image type through the header function, and then echo the image data.

Problem with php uploading images that cannot be displayed

Problem details:

php can upload files to the blob field of the database through a form , and then output. In fact, a better approach is to save the file to the server and only record the relevant information in the database, but you cannot always do what you want. No, I can only upload pictures to the database and then display them on the web page, but I encountered a problem: the pictures cannot be displayed.

The general method for uploading pictures to the database and then displaying them is to write a dedicated php page, obtain the picture id, query the database, and header function through the GET or POST method. Just output the image type and echo the image data. There is no problem with this method, but I have a problem. The image cannot be displayed.

I searched a lot of information on the Internet and tried many methods, but nothing worked. Finally, I saw an answer on stack overflow. Since it is in English, I will not quote the original sentence. The content isheaderThis function is a bit special. Be careful not to have other headers or other content before the header position. The result is this problem because I wrote a php file that contains some common Functions, including JavaScript functions, I just need to remove the require statement in the PHP file that outputs the image, and the image can be output.

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="html/text;charset=utf-8"/>
</head>
<body>
<form action="#"  name="form" method="post" enctype="multipart/form-data">
<p>
<input type="file" name="img" value="选择上传文件"/>
</p>
<input type="submit" value="上传"/>
</form>
</body>
</html>
<?php
date_default_timezone_set("PRC");         //设置时区
if(count($_FILES)>0){ 
$sort = array("image/jpeg","image/jpg","image/gif","image/pdg");
//判断是否是图片类型
if(in_array($_FILES[&#39;img&#39;][&#39;type&#39;],$sort)){ 
 $img = "img";    //获取上传到的文件夹位置
//判断文件夹是否存在 ,如果不存在创建一个
if(!file_exists($img)){
   mkdir("$img",0700);        //0700最高权限
}
$time=date("Y_m_d_H_i_s");     //获取当前时间
$file_name = explode(".",$_FILES[&#39;img&#39;][&#39;name&#39;]);         //$_FILES[&#39;img&#39;][&#39;name&#39;] 上传文件的名称 explode字符串打断转字符串
$file_name[0]=$time; 
$name = implode(".",$file_name);    //implode 把数组拼接成字符串
$img_name = "img/".$name;
if(move_uploaded_file($_FILES[&#39;img&#39;][&#39;tmp_name&#39;],$img_name)){   //move_uploaded_file 移动文件
   echo "<center><img style=&#39;width:1000px;&#39; src=&#39;$img_name&#39;>
   <p>
   <a href=&#39;img_uploading.php&#39;>重新上传</a></p></center>";
}else{
     echo "上传失败"; 
}
}else{ 
echo "不是图片类型";
}
}
?>
Copy after login

I have to say that programming is sometimes a very troublesome thing. Just because of a statement, it took me a day to try various methods; but programming is an interesting thing, because in When the problem is solved and the program runs successfully, the sense of accomplishment and satisfaction is truly unforgettable.

More learning tutorials: PHP image upload tutorial

The above is the detailed content of Problem with php uploading images that cannot be displayed. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template