简体   繁体   English

我的ajax有什么问题?

[英]What's wrong in my ajax?

I own a site called flopl.com. 我拥有一个名为flopl.com的网站。 It's pretty basic. 这很基本。 Upload your files, and get a URL back with a link to the file. 上载您的文件,然后获取带有该文件链接的URL。 But I wasn't the one coding it, and now I can't get help from the person who did anymore. 但是我不是那个人,现在我无法从做此事的人那里得到帮助。 There's something wrong in the code, but I don't know enough to figure out what. 代码中有错误,但是我不知道该怎么办。 So.. Here's the code: 所以..这是代码:

HTML: 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>

And the PHP: 和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;
}
?>

The progress-bar is showing, but I only get "Something went wrong" back. 显示进度条,但我只会看到“出现了问题”。 I've checked, the PHP.ini is set up correctly. 我已经检查过,PHP.ini设置正确。 The files are not uploading, and therefor also not giving me a URL back. 这些文件没有上传,因此也没有给我返回URL。

Could anyone figure out what's wrong? 有人能弄清楚怎么了吗?

Best regards, primarypanda 最好的问候,小熊猫

I found at least 2 problems with your project. 我发现您的项目至少有2个问题。

  1. Your host may allow upload big files but you need to check that file uploads are set or not in PHP.ini file. 您的主机可能允许上传大文件,但您需要检查PHP.ini文件中是否设置了文件上传。 in most of the CPanels, they allow to make custom php.ini file settings, here you need to set max_file_uploads = 20M or so. 在大多数CPanels中,它们允许进行自定义php.ini文件设置,在这里您需要设置max_file_uploads = 20M左右。

  2. using isset() function may cause some problem, I use !empty() function instead because it checks whether the variable is set and not empty both. 使用isset()函数可能会引起一些问题,我改用!empty()函数,因为它会检查变量是否已设置并且两个变量都不为空。 so you can do it in your code like... 因此您可以在代码中执行此操作,例如...

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

    You can notice that I have also checked that $_FILES['photoimg'] are also not empty, that means that the file is actually uploaded. 您可能会注意到,我还检查了$ _FILES ['photoimg']是否也不为空,这意味着该文件实际上已上传。

Best luck and Thank you 祝你好运,谢谢

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

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