razvan
razvan

Reputation: 61

Uploading Files and Images with mkdir specific location

What I am trying to put images where my folder is generate from "$tablename", but fail to bring the image there. How do I store the uploaded files ?

I like to upload images from my form.

<input type="file" id="file" name="files" multiple />

No matter which of those upload techniques I use is not good to use, to save the file to a specific location on the server.

If you have an idea how to do this problem please.

here is the mkdir code. The code is works fine.

 <?php
$tablename = "fisa";
$next_increment = 0;
$qShowStatus = "SHOW TABLE STATUS LIKE '$tablename'";
$qShowStatusResult = mysql_query($qShowStatus) or die("" . mysql_error() . "" . $qShowStatus);

$row = mysql_fetch_assoc($qShowStatusResult);
$next_increment = $row['Auto_increment'];

echo "$next_increment";

$tablename = (string) ("$next_increment");

$year = date("Y");
$month = date("m");
$day = date("d");
If (!file_exists($year)) {
    $createsyear = mkdir("$year", 0777);
} else {
    If (!file_exists("$year/$month")) {
        $createsmonth = mkdir("$year/$month", 0777);
    } else {
        If (!file_exists("$year/$month/$day")) {
            $createsday = mkdir("$year/$month/$day", 0777);
        } else {
            If (!file_exists($year / $month / $day / $tablename)) {
                $createsday = mkdir("$year/$month/$day/$tablename", 0777);
            } else {
                //dada
            }
        }
    }
}
?>

Thank You.

Upvotes: 1

Views: 1254

Answers (2)

razvan
razvan

Reputation: 61

I fix the problem

 <?php 
    $tablename      = "fisa";
    $next_increment     = 0;
    $qShowStatus        = "SHOW TABLE STATUS LIKE '$tablename'";
    $qShowStatusResult  = mysql_query($qShowStatus) or die ( "" . mysql_error() . "" . $qShowStatus );

    $row = mysql_fetch_assoc($qShowStatusResult);
    $next_increment = $row['Auto_increment'];

    echo "$next_increment";

    $tablename = (string)("$next_increment");
    $year = date("Y");
    $month = date("m");
    $day = date("d");

    If(!file_exists($year)){
    $createsyear = mkdir("$year", 0777);
    }
    else
    {
    If(!file_exists("$year/$month")){
    $createsmonth = mkdir("$year/$month", 0777);
    }
    else
    {
    If(!file_exists("$year/$month/$day")){
    $createsday = mkdir("$year/$month/$day", 0777);
    }
    else
    {
    If(!file_exists($year/$month/$day/$tablename)){
    $createsday = mkdir("$year/$month/$day/$tablename/", 0777);



    $valid_formats = array("jpg", "png", "gif", "zip", "bmp");
    $max_file_size = 1024*100; //100 kb

    $target = "$year/$month/$day/$tablename/"; 
    $count = 0;

    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
        // Loop $_FILES to execute 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 ($_FILES['files']['size'][$f] > $max_file_size) {
                    $message[] = "$name is too large!.";
                    continue; // Skip large files
                }
                elseif( ! 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], $target.$name)) {
                        $count++; // Number of successfully uploaded files
                    }
                }
            }
        }
    }

    }
    else
    {

    }
    }
    }


    }


    ?>

Upvotes: 0

Subin Thomas
Subin Thomas

Reputation: 1416

Here I give basic example for file upload.

in HTML

<form action="phpfilename.php" method="post" enctype="multipart/form-data" >
<input type="file" name="files" />
<input type="submit" name="submit" />
</form>

In PHP

<?php 
    if(isset($_POST['submit']))
    {
    $path = $_FILES["files"]["name"];
    $target = "$year/$month/$day/$tablename/$path";  //this is your path where image need to be saved
    move_uploaded_file($_FILES["files"]["tmp_name"],  $target);
    }
?>

Upvotes: 1

Related Questions