我的ajax有什么问题?

时间:2015-11-03 02:56:44

标签: php ajax url post upload

我拥有一个名为flopl.com的网站。这很基础。上传您的文件,然后返回一个带有该文件链接的URL。但我不是那个编码的人,现在我无法从那个做过的人那里得到帮助。代码中有问题,但我不知道弄清楚什么。所以..这是代码:

HTML:

    <!DOCTYPE html>
<html style="width:100%">
<head>
    <title>flopl</title>
    <link rel="stylesheet" href="style.css">
    <meta charset="utf-8" />
    <link rel="shortcut icon" href="http://flopl.com/images/favicon.ico" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<style>
.progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
.bar { background-color: #B4F5B4; width:0%; height:25px; border-radius: 3px; }
.percent { position:absolute; display:inline-block; top:-1px; left:48%; }
</style>
</head>
<body style="width:100%">
<div style="background-color:#2d2d2d; width: auto; height:150px; padding:-10px; margin-top:-20px;">
</div>
<div style="background-color:#8be1ab; width: auto; height:10px; padding:-10px; margin-top:0px;">
</div>
<center>

<img width="275px" src="images/logo.png" style="position:absolute; margin-top:-145px; margin-left:-145px;" />


</center>

<div style="width:400px; margin:auto; position:relative; margin-top:15%;" >
<center>
<div style="margin-top:90px; margin-left:25px;">
<p style="word-spacing:0.6px; font-family:Helvetica font-weight:light; margin-top:-20px; position:center;">Upload any type of file,</p>
<p style="word-spacing:0.6px; font-family:Helvetica font-weight:light; margin-top:-20px; position:center;">with no compression!</p>
</div>
</center>




<form id="imageform" method="post" enctype="multipart/form-data" action='ajax_image.php'>
<a href="#" class="buttonsub" onclick="document.getElementById('photoimg').click();"><img width="332px" src="images/upload.png" style="position:absolute; top:-120px; left:40px;"/></a>
<input type="file" name="photoimg" id="photoimg" style="padding: 0; margin: 0; display: none;" />
</form>

    <div class="progress" style="display: none;">
        <div class="bar"></div >
        <div class="percent">0%</div>
    </div>

        <div id="status"></div>
     <center>   

<script type="text/javascript" >
 $(document).ready(function() { 

            $('#photoimg').live('change', function() { 
                       $("#preview").html('');
                $("#preview").html('<img width="300" src="images/uploading.png" alt="Laddar upp...."/>');



    var bar = $('.bar');
    var percent = $('.percent');
    var status = $('#status');
    var progress = $('.progress');

    $('#imageform').ajaxForm({
    target:        '#preview',
        beforeSend: function() {
            status.empty();
            progress.show();
            var percentVal = '0%';
            bar.width(percentVal);
            percent.html(percentVal);
        },
        uploadProgress: function(event, position, total, percentComplete) {
            var percentVal = percentComplete + '%';
            bar.width(percentVal);
            percent.html(percentVal);
        },
        success: function() {
            var percentVal = '100%';
            bar.width(percentVal);
            percent.html(percentVal);
        },
        complete: function(xhr) {
            //status.html(xhr.responseText);
        }
    }).submit(); 




            });
        }); 
</script>




<div id='preview' style="margin-top:40px;">

</div>



</center>
</div>



</div>
<div class="footer" id="footer">
<div id="top">
  <img id="copyright" src="images/footer.png" alt="">
    <a id="logo" href="http://www.simplyvisual.se" target="_blank">
        <img width="150px" src="/images/SV.png" alt="Simply Visual">
    </a>
</div>
<!--<div class="footer" id="footer"><img style="margin-top:1%; margin-left:0%" width="150" src="images/SV.png" /><img style="margin-top:0%;" src="images/footer.png" />--></div>
</body>
</html>

PHP:

   <?php


session_start();
$session_id='1'; // User session id
$path = "upload/";


function url($url) {
   $url = preg_replace('~[^\\pL0-9_]+~u', '-', $url);
   $url = trim($url, "-");
   $url = iconv("utf-8", "us-ascii//TRANSLIT", $url);
   $url = strtolower($url);
   $url = preg_replace('~[^-a-z0-9_]+~', '', $url);
   return $url;
}

if(!empty($_POST) && !empty($_FILES['photoimg']) && $_SERVER['REQUEST_METHOD'] == 'POST')
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];

$pi = pathinfo($name);
$txt = $pi['filename'];
$ext = $pi['extension'];


$actual_image_name = time().$session_id.".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{

echo "Here's the link to your file: <a href='http://flopl.com/".$path.$actual_image_name."'>http://flopl.com/".$path.$actual_image_name."</a>";

}

} else {
echo "Something went wrong.";
exit;
}
?>

进度条正在显示,但我只回到“出了问题”。我已经检查过,PHP.ini设置正确。文件没有上传,因此也没有给我回网址。

有谁能弄明白什么是错的?

祝你好运, primarypanda

1 个答案:

答案 0 :(得分:0)

我发现你的项目至少有两个问题。

  1. 您的主机可能允许上传大文件,但您需要检查PHP.ini文件中是否设置了文件上传。在大多数CPanel中,它们允许自定义php.ini文件设置,在这里你需要设置max_file_uploads = 20M左右。

  2. 使用isset()函数可能会导致一些问题,我使用!empty()函数,因为它检查变量是否设置而不是两者都是空的。所以你可以在你的代码中这样做......

    if(!empty($_POST) && !empty($_FILES['photoimg']) && $_SERVER['REQUEST_METHOD'] == 'POST'){...}
    

    你可以注意到我还检查过$ _FILES ['photoimg']也不是空的,这意味着文件实际上传了。

  3. 祝你好运,谢谢你