[英]How to pass a file type input into javascript and use it into another php page?
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.
我在这里询问如何使用javascript传递文件类型,以便我可以将它用于另一个php页面。 The following is an input for type file:
以下是类型文件的输入:
<input type="file" name="imgCompany" id="imgCompany">
I am getting the file like this in php:我在 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?我要问的第一个问题是如何将 $file 变量放置在 javascript 函数中? (same php page).
(相同的 php 页面)。 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:第二个问题,我要问的是,一旦使用如下方法在 addImage() 函数中声明了该文件值,我该如何传递该文件值:
$.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.我将使用我从帖子传递到另一个 php 页面的内容,以便我可以将该图像上传到数据库中。 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>";
}
}
I cannot read JQuery (nor do I want to), but here are a few pointers:我无法阅读 JQuery(我也不想),但这里有一些提示:
The file name in file-upload: javascript-how-to-extract-filename-from-a-file-input-control 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:第二个问题,我要问的是,一旦使用如下方法在 addImage() 函数中声明了该文件值,我该如何传递该文件值:
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.
我不明白您要通过向其中添加 javascript 来完成什么。 The original filename will also be send to the server.
原始文件名也将发送到服务器。 (Inspect the $_FILES on arrival to see it for yourself)
(到达时检查 $_FILES 以亲自查看)
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.另一件事:如果您转到“另一页”,您的 $_FILES 将为空。 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.
我不确定你在做什么或如何做,但是当你看到一个空的 $_FILES 而你期望其中有一些信息时,请记住这一点。 :-)
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.