简体   繁体   English

一种形式的两个图像输入

[英]Two image inputs in one form

I have a form in which I collect user informations.我有一个收集用户信息的表格。 Its pretty big and it should have two inputs for image uploading.它相当大,它应该有两个用于图像上传的输入。 So it should have two images uploading inside one form.所以它应该在一个表单中上传两张图片。 I have a datebase where I store the image URL, and the first image input works perfectly, but I have no idea how can I now upload second image and store its url in the datebase.我有一个数据库,我在其中存储图像 URL,第一个图像输入工作完美,但我不知道现在如何上传第二个图像并将其 url 存储在数据库中。

My form:我的表格:

<form action="forms/credit.php" method="post" enctype="multipart/form-data">

<input type="file" name="file" class="input-text required-entry" required="">

<input type="file" name="cofile" class="input-text required-entry" required="">

<input type="submit" name="submit" value="Submit">

</form>

My php image handler:我的 php 图像处理程序:

$targetDir = "../images/";

$fileName = basename($_FILES["file"]["name"]);

$targetFilePath = $targetDir . $fileName;

$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){

    // Allow certain file formats

    $allowTypes = array('jpg','png','jpeg','gif','pdf');

    if(in_array($fileType, $allowTypes)){

        // Upload file to server

        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){

// Attempt insert query execution

$sql = "INSERT INTO credit (file, cofile) VALUES ('$fileName', '$fileName')";
if(mysqli_query($link, $sql) == false){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    exit;
} 

}
    }
    }

So as I said, it works fine for the first image but it doesn't work fine for second.所以正如我所说,它适用于第一张图像,但它不适用于第二张图像。 I guess I would need to use array or something like that, but can someone give me a hint or a code preview on how can I do it?我想我需要使用数组或类似的东西,但是有人可以给我一个提示或代码预览我该怎么做吗? I'm fairly new to PHP and I'm still learning a lot.我对 PHP 还很陌生,而且我还在学习很多东西。

Thanks!谢谢!

You need to store the file the same way you store $_FILES["file"] .您需要以与存储$_FILES["file"]相同的方式存储文件。 This can be accessed using $_FILES["cofile"]这可以使用$_FILES["cofile"]访问

Something along the lines of this should help (untested)类似的东西应该会有所帮助(未经测试)

$fileName = basename($_FILES["file"]["name"]);

$targetFilePath = $targetDir . $fileName;

$cofileName = basename($_FILES["cofile"]["name"]);

$targetcoFilePath = $targetDir . $cofileName;

if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath) &&  move_uploaded_file($_FILES["cofile"]["tmp_name"], $targetcoFilePath))

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

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