PHP上传图片

use*_*586 6 php upload image

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

但今天我说让我们做下一步,这将成为服务器的形象.我在YouTube上观看了3个视频,可能是100次只是重新编写代码,并以多种不同方式进行尝试.

http://www.youtube.com/watch?v=CxY3FR9doHI
http://www.youtube.com/watch?v=vFZfJZ_WNC4&feature=relmfu
Run Code Online (Sandbox Code Playgroud)

仍然无法得到它.

但长话短说:我有一个连接到服务器的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>
Run Code Online (Sandbox Code Playgroud)

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

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

我不知道该怎么办.

خال*_*مود 8

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

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

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


小智 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);
 ?>
Run Code Online (Sandbox Code Playgroud)

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