简体   繁体   English

PHP的jQuery图像上传

[英]php jquery image upload

i have a script that uploads an image into a folder instead of saving it as a blob.. 我有一个脚本,可以将图像上传到文件夹中,而不是将其另存为blob。

<?php
mysql_connect('localhost','root','')or die(mysql_error());
mysql_select_db('db_tourism')or die(mysql_error());
//$newname='baro.jpg';
//dir:[../../]
$uploaddir = '../../images/municipality/';
$cc=$uploaddir.$fileName;
$fileName = $_FILES['uploadfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$ext = end(explode('.', $fileName));
$newname=$fileName;//.'.'.$ext;

$file = $uploaddir .$newname; //basename($_FILES['uploadfile']['name']); 

if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 

 // echo "<script>alert('success:$fileName');</script>"; 

mysql_query("INSERT INTO `_temp-image` ( `id` , `File_name` , `path` ) 
VALUES (
NULL , '$fileName', '$file'
);");

 echo "success";
} 
else {
    echo "error";
}
?>

and here is the jquery 这是jQuery

    var btnUpload=$('#uploada');
    //var btnUploadTxt=$('#uploada').attr('value');
    var status=$('#status');
    new AjaxUpload(btnUpload, {
        action: 'upload-file.php',
        name: 'uploadfile',
        onSubmit: function(file, ext){
             if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                // extension is not allowed 
                btnUpload.val('Only JPG, PNG or GIF files are   allowed');
                return false;
            }
            btnUpload.val('Uploading...');
        },
        onComplete: function(file, response){
            //On completion clear the status
            btnUpload.val('Upload Picture');
            //Add uploaded file to list
            if(response==="success"){
                $('<li class="uplod" uid="_temp-image" title="click     to remove ['+file+']" id="'+file+'  "><span  id=" '+file+' " style="font-  family:calibri;font-size:10px;" >'+file+' [UPLOADED]</span></li>').appendTo('#uploaded');/  *.html('<img src="../uploaded_image/'+file+'" alt="" />'+file)*///.addClass('success');
            } else{
                $('<li></li>').appendTo('#uploaded').text(fi    le).addClass('error');
            }
        }
    });

it works fine i can add and delete picture... BUT my problem is handling DUPLICATE files... how to error trap if that kind of image is already uploaded?? 它可以正常工作,我可以添加和删除图片...但是我的问题是处理重复文件...如果已经上传了这种图像,如何错误陷阱?

Generate CRC checksum, MD5 or other hash type for image binary data and store that hash in database. 为图像二进制数据生成CRC校验和,MD5或其他哈希类型,并将该哈希存储在数据库中。

After upload - check new image checksum/hash and compare it with that stored in database. 上传后-检查新的图像校验和/哈希,并将其与数据库中存储的图像进行比较。

Use md5_file function. 使用md5_file函数。

$md5hash = md5_file(string $filename);

Here is more: http://www.php.net/manual/en/function.md5-file.php 这里是更多: http : //www.php.net/manual/en/function.md5-file.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM