PHP-Jquery图像上传不起作用 [英] PHP-Jquery image upload not working

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

问题描述

我正在尝试通过ajax发布表单数据

I'm trying to post form data through ajax

form1.php

我使用请求获取所有URL参数数据

I use request to get all URL parameter data

$_REQUEST["Ename"];
$_REQUEST["eImg"];

要上传图片,我使用以下代码 http ://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html

To upload the image,i use this code http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html

在上面的链接中,您可以在$ _FILES ['photoimg'] ['name'];的位置看到源代码,我使用$ _FILES ['image'] ['name'];但它不会上传文件并给出成功消息.

In the above link,you can see the source code,in the place of $_FILES['photoimg']['name'];,i use $_FILES['image']['name']; but it is not uploading the file and giving success message.

include('db.php');
session_start();
$session_id='1'; // User session id
$path = "uploads/";

我删除了标有**的脚本

I removed script that is marked with **

$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
    **if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {**

$name = $_FILES['image']['name'];
$size = $_FILES['image']['size'];
if(strlen($name)) {
  list($txt, $ext) = explode(".", $name);
  if(in_array($ext,$valid_formats)) {
    if($size<(1024*1024)) { // Image size max 1 Mb
      $actual_image_name = time().$session_id.".".$ext;
      $tmp = $_FILES['image']['tmp_name'];
      if(move_uploaded_file($tmp, $path.$actual_image_name)) {
        mysql_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");
        echo "<img src='uploads/".$actual_image_name."' class='preview'>";
      } else {
        echo "failed";
      } 
    } else {
      echo "Image file size max 1 MB";
    }
  } else {
    echo "Invalid file format.."; 
  }
}  **else {
  echo "Please select image..!";
  exit();
}**

推荐答案

要通过ajax上传图片,您应该使用iframe并将其ID设置为目标.

For upload image by ajax you should use an iframe and set its id to form target.

请看看 http://www.coursesweb.net/ajax/upload-images

上传图片是非常简单的代码

It is very simple code to upload image

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

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