-1

Please help me

My html code is as follows:

here image_name is getting through echo from another upload query

My upload script code

$path = "uploads/";

function getExtension($str) 
{
     $i = strrpos($str,".");
     if (!$i) { return ""; } 
     $l = strlen($str) - $i;
     $ext = substr($str,$i+1,$l);
     return $ext;
 }

$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
        $name = $_FILES['photoimg']['name'];
        $size = $_FILES['photoimg']['size'];

        if(strlen($name)){
                $ext = getExtension($name);
                if(in_array($ext,$valid_formats))
                {
                if($size<(1024*1024))
                    {
                        $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                        $tmp = $_FILES['photoimg']['tmp_name'];
                        if(move_uploaded_file($tmp, $path.$actual_image_name))
                            {
                        $time            =   time();
                        $ip                                    =   $_SERVER['REMOTE_ADDR'];
                mysql_query("INSERT INTO uploads(image_name,poster_user,created,cat,status,ip) VALUES('$actual_image_name','$u_id','$time', 'Photos', '1', '$ip')");
                                echo "<img src='uploads/".$actual_image_name."'  class='previewOfimgss'> "; 
                                $allimages_name = "$actual_image_name";
                                echo "$allimages_name";

                            }   
                        else
                            echo "Fail upload folder with read access.";
                    }
                    else
                    echo "Image file size max 1 MB";                    
                    }
                    else
                    echo "Invalid file format..";   
            }

        else
            echo "Please select image..!";

        exit;
    }

It is working quite good and giving result like

enter image description here

and i want images names in on textbox like

enter image description here

HiDeoo
  • 10,353
  • 8
  • 47
  • 47

2 Answers2

0
$items=$_POST["post_items"];
$final = "";
foreach($items as $item){

   $final .= $item."<br>";

}

echo $final

And you can then pass the $final variable to the column.

There is another way to do it.

 $items= $_POST["post_items"];
 $final = implode("<br>",$items);

It will work only if $items is array.

Kinshuk Lahiri
  • 1,468
  • 10
  • 23
0

Ok I've done a working solution for you. This is a kind of prototype of your system. Hope it helps you in what you are building.

fileForm.php (Where you select file to upload.)

<!DOCTYPE html>
<html>
    <head>

    </head>
    <body>
        <form action="uploadFile.php" method="post" enctype="multipart/form-data">
            <input type="file" name="photoimg[]" multiple="yes">
            <input type="submit" name="fileUploader">
        </form>
    </body>
</html>

uploadFile.php (Where you upload your files as in your question)

<?php
    if ($_SERVER["REQUEST_METHOD"]=="POST") {
        $path = "uploads/"; // Upload directory

        // Return's files extension
        function getExtension($str) 
        {
             $i = strrpos($str,".");
             if (!$i) { return ""; } 
             $l = strlen($str) - $i;
             $ext = substr($str,$i+1,$l);
             return $ext;
         }

        $valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP"); // Valid formats to upload

        $fileCount=count($_FILES["photoimg"]["name"]); // Number of files uploaded
        $files=array(); // Initilize an empty array to save names 
        // Loop through all files and upload them
        for ($i=0; $i < $fileCount; $i++) { 
            $name=$_FILES["photoimg"]["name"][$i];
            $tmp=$_FILES["photoimg"]["tmp_name"][$i];
            $size=$_FILES["photoimg"]["size"][$i];

            // If name is not empty
            if(!empty($name)){
                $ext = getExtension($name); // Get file extension
                // If file is valid to upload
                if(in_array($ext,$valid_formats)){
                    If file is less than 1 MB.
                    if($size<(1024*1024)){
                        $actual_image_name = time().substr(str_replace(" ", "_", $name), 5);        // Final name of image
                        // If file uploads successfully
                        if(move_uploaded_file($tmp, $path.$actual_image_name)){
                            $time=time();
                            $ip=$_SERVER['REMOTE_ADDR'];
                            mysql_query("INSERT INTO uploads(image_name,poster_user,created,cat,status,ip) VALUES('$actual_image_name','$u_id','$time', 'Photos', '1', '$ip')"); // Insert into your table
                            echo "<img src='uploads/$actual_image_name'  class='previewOfimgss'> ";  // Show the image
                            $files[$i] = $actual_image_name;     // Save file names
                        }else{
                            echo "Fail upload folder with read access.";
                        }
                    }else{
                        echo "Image file size max 1 MB";                    
                    }
                }else{
                    echo "Invalid file format..";   
                }
            }else{
                echo "Please select image..!";
            }
        }
    }
?>
<form action="toSaveFileName.php" method="post">
<?php
    for ($i=0; $i < $fileCount; $i++) { 
        // Generate input fields
        echo "<input type='text' name='post_items[]' value='{$files[$i]}'>";
    }
?>
    <input type="submit">
</form>

toSaveFileName.php (This is what you originally asked for.)

$items=$_POST["post_items"]; // from input fields
$todb=""; // to send to database
if(is_array($items)){
    $todb=implode("<br>",$items);
}else{
    $todb=$items;
}
echo $todb; // for output
//save to database

Now implementing it to your system is your job. And I hope you should be able to do it on your own.

Don't forget to mark this as answer and vote up.

Anish Silwal
  • 980
  • 1
  • 10
  • 24