PHP上传图片 [英] PHP upload image

查看:62
本文介绍了PHP上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有很多时间可以花在这上面.我是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()

我不知道该怎么办.

解决方案

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

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

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

Alright I have way to much time invested in this. I am new to PHP programming and trying to grasp the basics, but I am a little lost as of last night I was able to get a PHP form to upload basic data like a name address and stuff to my (MySQL) server.

But today I said let's do the next step which would be an image to the server. I have watched 3 videos on YouTube probably a 100 times just recoping code and trying it in so many different ways.

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

and still haven't been able to get it.

But long story short: I have a config.php file that connects to the server and here is the the code I'm running on the upload form page:

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

The reason for all the '', '', '', '' on the insert line is because I have the name in the 10th field and the image blob in the 11th and all the ones leading up to that are first name, last name and random stuff like that. How can I fix this? It is returning the error:

Fatal error: Call to undefined function file_get_content() in /home/content/34/9587634/html/WEBPAGE/UploadContent.php on line 22

I don't know what to do.

解决方案

The code overlooks calling the function move_uploaded_file() which would check whether the indicated file is valid for uploading.

You may wish to review a simple example at:

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

这篇关于PHP上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆