Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have problem with my script. It uploads image in my hosting server. There created folders with images are missing from hosting server next day or next week not all images of course - 18-19% of all for 2 weeks. I asked them. They told me- the problem is in my php code. If someone can help I will be happy. May be I add some 0777 in mkdir()???? I don't know. I am thinking at the all time...

<?php

if (isset($_FILES['myfile'])) {
    $ip = getRealIpAddr();
    $usernameupload = $user_data['username'];

    $title = sanitize($_POST['title']);
    $description = sanitize($_POST['description']);
    if (empty($_POST['folder']) === true) { $folder = sanitize($_POST['folder_option']);} else { $folder = sanitize($_POST['folder']);}
    $date = date('M-d-Y');
    $code = "";

    $errors = array();
    $allowed_ext =array('jpg', 'jpeg', 'png', 'gif');

    $file_name = $_FILES['myfile']['name'];
    $file_size = $_FILES['myfile']['size'];
    $file_tmp = $_FILES['myfile']['tmp_name'];
    $extension=explode('.',$file_name); 
    $file_ext=strtolower(end($extension));

    if (isset($_POST['submitupload'])) {
        if ($_FILES['myfile']['name'] == ""){
        $errors[] = ' *Upload file';
        } else if (in_array($file_ext, $allowed_ext) === false) {
        $errors[] = ' *Extension not allowed';
        }
    }

    if ($file_size > 5042880) {
        $errors[] = 'File size must be under 5mb';
        }
    if (strlen($folder) > 50) {
        $errors[] = 'Your new folder name must be max 50 characters';
        }

    if (empty($_POST['title'])) {
        $errors[] = 'Please Fill Title';
        }

    if (strlen($_POST['title']) > 255) {
        $errors[] = 'Your title must be max 255 characters';
        }

    if (empty($errors)) {

        require('db.inc.php');

        $charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        $length = 50;

        for ($i = 0; $i <= $length; $i++) {
            $rand = rand() % strlen($charset);
            $tmp = substr($charset, $rand, 1);
            $code .= $tmp;
            }

        $query = mysql_query("SELECT code FROM files WHERE code='$code'");
        $numrows = mysql_num_rows($query);

        while ($numrows != 0) {
                for ($i = 0; $i <= $length; $i++) {
                $rand = rand() % strlen($charset);
                $tmp = substr($charset, $rand, 1);
                $code .= $tmp;
                }
        $query = mysql_query("SELECT code FROM files WHERE code='$code'");
        $numrows = mysql_num_rows($query);
        }

        mkdir("files/$code");
        mkdir("files/thumbs/$code");


        if (move_uploaded_file($file_tmp, "files/$code/".$file_name)) {

        $file = "";     
        $query = mysql_query("INSERT INTO files VALUES ('', '$usernameupload', '$ip', '$title', '$code', '$folder', '$description', '$file_name', '$file_ext', '$file_size', '$date')");

        $query1 = mysql_query("INSERT INTO votes VALUES ('', '', '', '$code')");

        create_thumb('files/'.$code.'/', $file_name, 'files/thumbs/' . $code . '/');


        header("Location: userdownload.php?code=$code");
        exit();

            }
        }
    }
?>

<form action="" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td id="editfill">Image:*</td>
            <td><input type="file" name="myfile" id="fileup"></td>
            <tr>
            <td></td>
            <td><?php 

            if (isset($_POST['submitupload'])) {
        if ($_FILES['myfile']['name'] == ""){
        echo '<div id="errormsg"> *Upload file </div>';
        } else if (in_array($file_ext, $allowed_ext) === false) {
        echo '<div id="errormsg"> *Allowed extension: .jpg, .jpeg, .png, .gif</div>';
        } else if ($file_size > 5042880) {
        echo '<div id="errormsg"> *File size must be under 5mb </div>';
        }
    }
    ?></td>
    </tr>
        </tr>
        <tr>
            <td id="editfill">Title:*</td>
            <td><input type="text" name="title" id="title" value="<?php if (isset($_POST['submitupload'])) echo htmlentities($_POST['title']); ?>" maxlength="255"></td>
        <tr>
        <td></td>   
            <td><?php 
            if (isset($_POST['submitupload'])) {
            if (empty($_POST['title'])) {
        echo '<div id="errormsg"> *Please fill title </div>';
        }
            if (strlen($_POST['title']) > 255) {
        echo '<div id="errormsg"> *Your title must be max 255 characters</div>';
        }
        }
            ?></td>
         </tr>
        </tr>
         <tr>
            <td id="editfill">Create Album:</td>
            <td><input type="text" name="folder" id="title" value="<?php if (isset($_POST['submitupload'])) echo htmlentities($_POST['folder']); ?>" maxlength="50">
            </td>
        <tr>
        <td></td>
        <td><?php   
        if (strlen($folder) > 50) {
        echo '<div id="errormsg"> *Your new album name must be max 50 characters</div>';
        }
 ?></td>
        </tr>       
        </tr>
        <tr>
            <td id="editfill">Your Albums:</td>
            <td>
            <select name="folder_option" class="select">
            <option></option>
        <?php  
        $mysql_folder = mysql_query("SELECT `folder_name` FROM `files` WHERE `username` = '$usernameupload' AND `folder_name` > '' GROUP BY `folder_name` ORDER BY `folder_name` ASC"); 

        while ($query_row = mysql_fetch_array($mysql_folder)) {
                $filefolder = $query_row['folder_name'];

                echo '<option value="' . $filefolder . '">' . $filefolder . '</option>';
                }

        ?>
    </select>
            </td>
        <tr>
        <td></td>
        <td></td>
        </tr>       
        </tr>
        <tr>
            <td id="editfill">Description:</td>
            <td><textarea name="description" id="description"><?php if (isset($_POST['submitupload'])) echo htmlentities($_POST['description']); ?></textarea></td>
        <tr><td></td>
            <td>
        </td>
        </tr>       
        </tr>
        <tr>
            <td></td>
            <td><center><br><input type="submit" name="submitupload" id="submitupload" value="Upload"></center></td>
        </tr>
    </table>
</form>
share|improve this question
are you saying some of the photos you upload from the previous 2 weeks are missing? – Kaii Mar 14 at 7:19
Еxactly. They are missing with created folder and image inside. – user1929805 Mar 14 at 7:23
1  
you doesn`t define any file deletion in this case and thats very impossible for this code to delete files that created .. Except when you are trying to create folder then insert the images .. maybe it comes up with an error while creating the folder thats why it stops and the folder and images wouldn`t be created but still inserted at database.. – Kaii Mar 14 at 7:29
I know. I don't explain to myself how is possible but ... All is possible. There some of the images just are missing next day or next week from my hosting server. – user1929805 Mar 14 at 7:32
add comment (requires an account with 50 reputation)

1 Answer

up vote 1 down vote accepted

Try this one to efficiently see if mkdir creation doesnt failed. else folder and image surely will not be exist even it is added unto your database.

//previous code...
mkdir("files/$code");
mkdir("files/thumbs/$code");

//change to this...
if (!mkdir("files/$code", 0777, true)) {
    die('Failed to create folders...');
}

if (!mkdir("files/thumbs/$code", 0777, true)) {
    die('Failed to create sub folders...');
}
share|improve this answer
Uploaded image and no problems with uploading – user1929805 Mar 14 at 8:07
Thanks for support. Is it possible my problem will be from 0777, true??? – user1929805 Mar 14 at 8:09
I must wait now for result. – user1929805 Mar 14 at 8:11
1  
i think so.. rofl.. Cause I don`t see anything else wrong at your code.., but better test it for proven and clear results :).. – Kaii Mar 14 at 8:13
Thank you for support my friend. I'll buy you bottle beer thanks again – user1929805 Mar 14 at 8:16
show 2 more commentsadd comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.