简体   繁体   中英

Script for uploading multiple images and add path to database

I have a school project for computer science and right now I'm struggling with uploading multiple images to the server and adding the path to the database. I've been looking around here and elsewhere for solutions, but they were either incomplete or too complex for me, which means I can't 'translate' them into my own situation. I have found a script for adding a single picture to the server and adding the path/name to the database, which I've edited a bit:

<?php

$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);
$name = str_replace(' ', '_', $name);

$target = "images/";
$target = $target . $name . '/';
if (!file_exists($target))
{
    mkdir('images/' . $name . '/', 0777, true);
}
$target = $target . basename( $_FILES['photo']['name']);

$file_type = $_FILES['photo']['type']; //returns the mimetype
//print ($file_type);
$allowed = array("image/jpeg", "image/gif", "image/png");
if(!in_array($file_type, $allowed)) {
    $error_message = 'Only jpg, gif, and png files are allowed.';
    $error = 'yes';
    print($error_message);
}
else {
    mysql_connect("localhost", "username", "password") or die(mysql_error());
    mysql_select_db("addimg") or die(mysql_error()) ;
    mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')");

    //Writes the photo to the server
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
    {

        //Tells you if its all ok
        echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
    }
    else {

        //Gives and error if its not
        echo "Sorry, there was a problem uploading your file.";
    }
}
?>

Also, I've found a script to upload multiple images, but it doesn't put anything into the database:

<?php
$valid_formats = array("jpg", "png", "gif", "jpeg");
//$max_file_size = 1024*100; //100 kb
$path = "images/"; // Upload directory
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    // Loop $_FILES to exeicute all files
    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['error'][$f] == 4) {
            continue; // Skip file if any error found
        }          
        if ($_FILES['files']['error'][$f] == 0) {              
            if(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            else{ // No error found! Move uploaded files 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                $count++; // Number of successfully uploaded file
            }
        }
    }
}
?>

Now I hope there is a way to somehow combine these scripts or find another way, in which case I might need to show what is exactly what I need. (Maybe I should've anyway...)

Thanks,

Vasilis

if your files are uploading as will then you can do update youe code like this example to add picture title into Database ....

<?php
$valid_formats = array("jpg", "png", "gif", "jpeg");
//$max_file_size = 1024*100; //100 kb
$path = "images/"; // Upload directory
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$name = str_replace(' ', '_', $name);

    // Loop $_FILES to exeicute all files
    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['error'][$f] == 4) {
            continue; // Skip file if any error found
        }          
        if ($_FILES['files']['error'][$f] == 0) {              
            if(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            else{ // No error found! Move uploaded files 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))

                // Insert into Database
                $pic = $path.$name;
                $query = mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") or die(mysql_error());
                if($query) {
                    $count++; // Number of successfully uploaded file
                }

            }
        }
    }
    echo $count .' Files uploaded';
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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