douxing5199 2015-07-06 13:54
浏览 47

如何将文件类型输入传递给javascript并将其用于另一个php页面?

Hello and good day everyone! I am here to ask on how to pass a file type using javascript, so that I can use it to another php page. The following is an input for type file:

<input type="file" name="imgCompany" id="imgCompany">

I am getting the file like this in php:

$file = $_FILES['imgCompany']['tmp_name'];

The first question I have to ask is how can I get the $file variable to be place in a javascript function? (same php page). So far this was my attempt to do so:

function addImage() {
  var companyImage = $('#imgCompany').val();
}

The second question I have to ask how do I pass that file value once it is declared in the addImage() function using a method like this:

    $.post('addImage.php', {postimage:file},
    function(data)
    {
        //returns the echoed value from the php file
        $('#result').html(data);
    }); 

Or is there another way?

I am going to use what I pass from the post to another php page, so I can upload that image into a database. I am uploading this image into a database like this:

        $image = addslashes (file_get_contents($_FILES['image']['tmp_name']));
        //$image_name = addslashes ($_FILES['image']['name']);
        $image_size = getimagesize($_FILES['image']['tmp_name']);

        if ($image_size == FALSE)
            echo "<h3 class='warningStyle' align='center' style='position:absolute; top:235px; left:660px;'>No Image was Selected</h3>";
        else {

            if (!$insert = $db->query ("INSERT INTO company VALUES('','$companyName','$image')"))
                echo "<h3 class='warningStyle' align='center' style='position:absolute; top:235px; left:650px;'>Problem uploading the image</h3>";
        else {
            echo "<h3 class='successStyle' align='center' style='position:absolute; top:235px; left:620px;'>Company Account successfully created</h3>";
        }

    }
  • 写回答

1条回答 默认 最新

  • dongqiao1964 2015-07-06 14:13
    关注

    I cannot read JQuery (nor do I want to), but here are a few pointers:

    The file name in file-upload: javascript-how-to-extract-filename-from-a-file-input-control

    The second question I have to ask how do I pass that file value once it is declared in the addImage() function using a method like this:

    You don't have to, the file will be send in the form you are sending. I do not understand what you are trying to accomplish by adding javascript to that. The original filename will also be send to the server. (Inspect the $_FILES on arrival to see it for yourself)

    About the insertion in the database:

    addslashes? Really? Escape it in another way, preferable with the quotation/escape functionality delivered with your database.

    And another thing: If you go to "another page" your $_FILES will be empty. Handle it on arrival, not after invoking a new page. I am not sure what or how you are doing it, but keep that in mind when you see an empty $_FILES while you expected some information in it. :-)

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line