通过使用jQuery提交multipart / form-data的问题

时间:2011-03-04 19:34:52

标签: php jquery

我通过提交带有multipart / form-data的表单有问题,所有这些都来到数据库,但文件名没有,文件没有上传。

这是代码: 文件1表格:

    <?php
require_once("functions.php"); // calling generate_options
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../css/form.css" type="text/css" />  
<script type="text/javascript" src="../libs/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../libs/data.js"></script>
</head>
<body>
<div id="container">
<form id="form" name="form" method="post">
<ul>
<li>
<label class="desc">&raquo; First:</label>
<div><input id="first" name="first" type="text" class="field text medium" value="" maxlength="255"/></div>
</li>
<li><label class="desc">&raquo; years:</label>
<div><select id="years" name="years" class="field text medium"><option value="0">Izaberite:</option><?=generate_options(date('Y'),1900)?></select></div>
</li>
<li><label class="desc">&raquo; Options:</label>
<div><select id="option" name="option" class="field text medium"><option value="0" selected="selected">Select:</option><option value="1">option1</option><option value="2">option2</option>    </select></div>
</li>
<li>
<label class="desc">&raquo; options:</label>
<div><select id="option2" name="option2" class="field text medium"><option value="0" selected="selected">Select:</option>
<option value="1">option1</option>
<option value="2">option2</option>
<option value="3">option3</option>
</select>
</div>
</li>
<li>
<label class="desc">&raquo; email or phone:</label>
<div><input id="emorph" name="emorph" type="text" class="field text medium" value="" maxlength="255"/></div>
</li>
<li>
<label class="desc">&raquo; location:</label>
<div><input id="location" name="location" type="text" class="field text medium" value="" maxlength="255"/></div>
</li>
<li><label class="desc">&raquo; photo:</label>
<div><input id="file" name="file" type="file" class="field text medium" size="45" />
</div>
</li>
<li>
<label class="desc">&raquo; additional info:</label>
<div><textarea name="additional" id="addtional" cols="45" rows="3" class="field text medium"></textarea>
</div>
</li>
<li><label class="desc">I agree with <a href="#" target="_blank">terms of use</a> <input name="checkbox" type="checkbox" id="checkbox"/></label></li>
</ul>
<div class="buttons">
<input  type="submit" value="Submit" class="submit"/>
</div>
<span class="error" style="display:none"> neki od podataka nije ispravan</span>
<span class="licence" style="display:none"> licence </span>
<span class="success" style="display:none"> uspesno</span>
<span class="kontakt" style="display:none"> kontakt nije tacan</span>
</form>
</div>
</body>
</html>

第二个文件:data.js

$(function() {
$(".submit").click(function() {
 $( "#form" )
    .attr( "enctype", "multipart/form-data" )
    .attr( "encoding", "multipart/form-data" );                                                     
    var first = $("#first").val();
    var years = $("#years").val();
    var option = $("#option").val();
    var option2 = $("#option2").val();
    var emorph = $("#emorph").val();
    var location = $("#location").val();
    var file = document.getElementById("file"); // PHOTO WHICH I NEED TRANSFERING TO personalads.php ALSO IS IN dataString
    var additional = $("#additional").val();
    var check = $("#checkbox").attr('checked');
if(check) {
    var dataString = 'first='+ first + '&years=' + years + '&option=' + option + '&option2=' + option2 + '&emorph=' + emorph + '&location=' + location + '&file=' + file + '&additional=' + additional;
    $('.licence').fadeOut(200).hide();
    var specfilter = /^([<>])+$/; // DON'T ALLOW SCRIPT TAGS
if(first=='' || years=='0' || option=='0' || option2=='0' || emorph=='' || location=='0' || first.match(specfilter) || location.match(specfilter)  || additional.match(specfilter)) {
    $('.success').fadeOut(200).hide();     
    $('.error').fadeOut(200).show();
                                                                                       }
else {
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; // EMAIL FILTER
    var numfilter = /^[0-9]+$/; // NUMERIC FILTER
if ((emorph.match(filter)) || (emorph.match(numfilter))) {
    $.ajax({
    type: "POST",
    url: "processit.php",
    data: dataString,
    dataType: "html",   
    success: function(){
                $('.success').fadeIn(200).show();
                $('.error').fadeOut(200).hide();
                $('.licence').fadeOut(200).hide();
                $('.kontakt').fadeOut(200).hide();                  
                       }
           });
                                                           }
else {
    $('.success').fadeOut(200).hide();     
    $('.error').fadeOut(200).hide();
    $('.kontakt').fadeOut(200).show(); 
     }  
    }
          }
else {
    $('.licence').fadeOut(200).show();
     }
return false;
});
  });

第三名:processit.php

<?php
 if($_POST) {
// Find location for mysql connection function
require_once("functions.php");
// Connect to the database
connect_db();

// Get arrived informations from data.js
$first = $_POST['first'];
$years = $_POST['years'];
$option = $_POST['option'];
$option2 = $_POST['option2'];
$emorph = $_POST['emorph'];
$location = $_POST['location'];
$additional = $_POST['additional'];
$status = '0'; // Default locking option it`s been changed to 1 after email verification and account will be useable

// Setting up photo
$targetFile = str_replace('//','/',$targetPath) . $_FILES['file']['name'];  
$tempFile = $_FILES['file']['tmp_name'];
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); 
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
$size=filesize($_FILES['file']['tmp_name']);
if($extension=="jpg" || $extension=="jpeg" )
    {
    $tempFile = $_FILES['file']['tmp_name'];
    $src = imagecreatefromjpeg($tempFile);
    }
    else if($extension=="png")
    {
    $tempFile = $_FILES['file']['tmp_name'];
    $src = imagecreatefrompng($tempFile);
    }
    else 
    {
    $src = imagecreatefromgif($tempFile);
    }
    list($width,$height)=getimagesize($tempFile);
    $newwidth=180;
    $newheight=135;
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    $newFileName = md5($tempFile).'.'.$ext; 
    $filename = "../galleries/users/". $newFileName;
    imagejpeg($tmp,$filename,100);
    move_uploaded_file($tempFile,$filename);
    imagedestroy($src);
    imagedestroy($tmp);

$thumb = 'NONE'; // NONE FOR THIS TIME 
//Call mysql_query and insert data
$query = mysql_query("INSERT INTO personal_ads (first,years,option,option2,emorph,location,thumb,fotka,additional,status) VALUES
('". mysql_real_escape_string($first) ."',
 '". mysql_real_escape_string($years) ."',
 '". mysql_real_escape_string($option) ."',
 '". mysql_real_escape_string($option2) ."',
 '". mysql_real_escape_string($emorph) ."', 
 '". mysql_real_escape_string($location) ."',
 '". mysql_real_escape_string($thumb) ."',  
 '". mysql_real_escape_string($newFileName) ."',
 '". mysql_real_escape_string($additional) ."',                  
 '". mysql_real_escape_string($status) ."')")
or die(mysql_error());
     }else { }
   ?>

错误确切地说,在需要插入图片的行中,我有d41d8cd98f00b204e9800998ecf8427e.,google说这是我用于更改图片名称的md5,此哈希是字符串“”的哈希值。同样具有相同名称的文件也会上载到目录中。有人可以帮忙吗? 非常感谢...

1 个答案:

答案 0 :(得分:0)

var file = document.getElementById("file"); // PHOTO WHICH I NEED TRANSFERING TO 

它无效。

相关问题