Home >Backend Development >PHP Tutorial >Problem with php uploading images that cannot be displayed
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 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 isheader
This 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['img']['type'],$sort)){ $img = "img"; //获取上传到的文件夹位置 //判断文件夹是否存在 ,如果不存在创建一个 if(!file_exists($img)){ mkdir("$img",0700); //0700最高权限 } $time=date("Y_m_d_H_i_s"); //获取当前时间 $file_name = explode(".",$_FILES['img']['name']); //$_FILES['img']['name'] 上传文件的名称 explode字符串打断转字符串 $file_name[0]=$time; $name = implode(".",$file_name); //implode 把数组拼接成字符串 $img_name = "img/".$name; if(move_uploaded_file($_FILES['img']['tmp_name'],$img_name)){ //move_uploaded_file 移动文件 echo "<center><img style='width:1000px;' src='$img_name'> <p> <a href='img_uploading.php'>重新上传</a></p></center>"; }else{ echo "上传失败"; } }else{ echo "不是图片类型"; } } ?>
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!