使用ajax mysql php上传图片

新手上路,请多包涵

我想尝试使用 php 和 mysql 上传图像。我正在使用表单使用 ajax 发送数据。

我的 HTML 代码:

 <input type="file" name="logo" id="logo" class="styled">
<textarea rows="5" cols="5" name="desc" id="desc" class="form-control"></textarea>
<input type="submit" value="Add" id="btnSubmit" class="btn btn-primary">

阿贾克斯代码:

 var formData = new FormData($("#frm_data")[0]);
$("#btnSubmit").attr('value', 'Please Wait...');
$.ajax({
    url: 'submit_job.php',
    data: formData,
    cache: false,
    contentType:false,
    processData:false,
    type: 'post',
    success: function(response)

我的 PHP 代码( submit_job.php ):

 $desc =  mysqli_real_escape_string($con, $_POST['desc']);
$date = date('Y-m-d H:i:s');
$target_dir = "jobimg/";
$target_file = $target_dir . basename($_FILES["logo"]["name"]);
move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file);

原文由 Meena patel 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 314
2 个回答

尝试这个:

查询:

 $('#upload').on('click', function() {
        var file_data = $('#pic').prop('files')[0];
        var form_data = new FormData();  // Create a FormData object
        form_data.append('file', file_data);  // Append all element in FormData  object

        $.ajax({
                url         : 'upload.php',     // point to server-side PHP script
                dataType    : 'text',           // what to expect back from the PHP script, if anything
                cache       : false,
                contentType : false,
                processData : false,
                data        : form_data,
                type        : 'post',
                success     : function(output){
                    alert(output);              // display response from the PHP script, if any
                }
         });
         $('#pic').val('');                     /* Clear the input type file */
    });

PHP:

 <?php
    if ( $_FILES['file']['error'] > 0 ){
        echo 'Error: ' . $_FILES['file']['error'] . '<br>';
    }
    else {
        if(move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']))
        {
            echo "File Uploaded Successfully";
        }
    }

?>

原文由 Mayank Pandeyz 发布,翻译遵循 CC BY-SA 4.0 许可协议

安全性是网页设计的重要组成部分。尝试以下验证以获得更高的安全性。

检查 $_FILES 中的文件

if (empty($_FILES['image']))
    throw new Exception('Image file is missing');

检查上传时间错误

if ($image['error'] !== 0) {
    if ($image['error'] === 1)
        throw new Exception('Max upload size exceeded');

    throw new Exception('Image uploading error: INI Error');
}

检查上传的文件

if (!file_exists($image['tmp_name']))
    throw new Exception('Image file is missing in the server');

检查文件大小

$maxFileSize = 2 * 10e6; // = 2 000 000 bytes = 2MB
if ($image['size'] > $maxFileSize)
    throw new Exception('Max size limit exceeded');

验证图像

$imageData = getimagesize($image['tmp_name']);
if (!$imageData)
    throw new Exception('Invalid image');

验证 MIME 类型

$mimeType = $imageData['mime'];
$allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($mimeType, $allowedMimeTypes))
    throw new Exception('Only JPEG, PNG and GIFs are allowed');

希望这可以帮助其他人创建没有安全问题的上传 PHP 脚本。

资源

原文由 Supun Kavinda 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题