mkdir and upload problem - php

Given code creates the required directory with 777 permission but do not upload files
if i create the folder from filezilla ftpclient or change chown from ssh it uploads file

why that happens ?
3XLcomAsked:
Who is Participating?
 
Ray PaseurCommented:
This script worked for me.  I have disabled it on my server for obvious reasons, but you might want to install it on your server and test it.

Best regards, ~Ray
<?php // RAY_temp_3xlcom.php
error_reporting(E_ALL);


// MANUAL REFERENCE PAGES YOU MUST UNDERSTAND TO UPLOAD FILES
// http://php.net/manual/en/features.file-upload.php
// http://php.net/manual/en/features.file-upload.common-pitfalls.php
// http://php.net/manual/en/function.move-uploaded-file.php


// PHP 5.1+  SEE http://php.net/manual/en/function.date-default-timezone-set.php
date_default_timezone_set('America/Chicago');

// ESTABLISH THE NAME OF THE 'uploads' DIRECTORY
$uploads = 'RAY_junk';

// ESTABLISH THE BIGGEST FILE SIZE WE CAN ACCEPT - ABOUT 8 MB
$max_file_size = '8192000';

// ESTABLISH THE MAXIMUM NUMBER OF FILES WE CAN UPLOAD
$nf = 1;

// ESTABLISH THE KINDS OF FILE EXTENSIONS WE CAN ACCEPT
$file_exts = array
( 'jpg'
, 'gif'
, 'png'
, 'txt'
, 'pdf'
)
;

// LIST OF THE ERRORS THAT MAY BE REPORTED IN $_FILES[]["error"] (THERE IS NO #5)
$errors = array
( 0 => "Success!"
, 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini"
, 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"
, 3 => "The uploaded file was only partially uploaded"
, 4 => "No file was uploaded"
, 5 => "UNDEFINED ERROR"
, 6 => "Missing a temporary folder"
, 7 => "Cannot write file to disk"
)
;




// IF THERE IS NOTHING IN $_POST, PUT UP THE FORM FOR INPUT
if (empty($_POST))
{
    ?>
    <h2>Upload <?php echo $nf; ?> file(s)</h2>

    <!--
        SOME THINGS TO NOTE ABOUT THIS FORM...
        ENCTYPE IN THE HTML <FORM> STATEMENT
        MAX_FILE_SIZE MUST PRECEDE THE FILE INPUT FIELD
        INPUT NAME= IN TYPE=FILE DETERMINES THE NAME YOU FIND IN $_FILES ARRAY
        ABSENCE OF ACTION= ATTRIBUTE IN FORM TAG CAUSES POST TO SAME SCRIPT
    -->

    <form name="UploadForm" enctype="multipart/form-data" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
    <p>
    Find the file(s) you want to upload and click the "Upload" button below.
    </p>

    <?php // CREATE INPUT STATEMENTS FOR UP TO $n FILE NAMES
    for ($n = 0; $n < $nf; $n++)
    {
        echo "<input name=\"userfile$n\" type=\"file\" size=\"80\" /><br/>\n";
    }
    ?>
    <br/>Directory name <input autocomplete="off" name="dirname" />
    <br/>Check this box <input autocomplete="off" type="checkbox" name="overwrite" /> to <strong>overwrite</strong> existing files.
    <input type="submit" value="Upload" />
    </form>
    <?php
    die();
}
// END OF THE FORM SCRIPT



// WE HAVE GOT SOMETHING IN $_POST - RUN THE ACTION SCRIPT
else
{
    // THERE IS POST DATA - PROCESS IT
    echo "<h2>Results: File Upload</h2>\n";

    // SEE WHAT IS COMING THROUGH FROM THE FORM
    echo "<pre>"; var_dump($_FILES); var_dump($_POST); echo "</pre>\n";

    // SEE THE CURRENT WORKING DIRECTORY
    var_dump(getcwd());

    // SYNTHESIZE THE DIRECTORY NAME
    clearstatcache();
    $uploads = $uploads . $_POST["dirname"];
    if (!is_dir( (getcwd() . DIRECTORY_SEPARATOR . $uploads) ) )
    {
        mkdir( getcwd() . DIRECTORY_SEPARATOR . $uploads);
    }

    // ITERATE OVER THE CONTENTS OF $_FILES
    foreach ($_FILES as $my_uploaded_file)
    {
        // SKIP OVER EMPTY SPOTS - NOTHING UPLOADED
        $error_code    = $my_uploaded_file["error"];
        if ($error_code == 4) continue;

        // SYNTHESIZE THE NEW FILE NAME
        $f_type    = trim(strtolower(end    (explode( '.', basename($my_uploaded_file['name'] )))));
        $f_name    = trim(strtolower(current(explode( '.', basename($my_uploaded_file['name'] )))));
        $my_new_file = getcwd() . '/' . $uploads . '/' . $f_name . '.' . $f_type;
        $my_file     =                  $uploads . '/' . $f_name . '.' . $f_type;

        // OPTIONAL TEST FOR ALLOWABLE EXTENSIONS
        if (!in_array($f_type, $file_exts)) die("Sorry, $f_type files not allowed");

        // IF THERE ARE ERRORS
        if ($error_code != 0)
        {
            $error_message = $errors[$error_code];
            die("Sorry, Upload Error Code: $error_code: $error_message");
        }

        // GET THE FILE SIZE
        $file_size = number_format($my_uploaded_file["size"]);

        // IF THE FILE IS NEW (DOES NOT EXIST)
        if (!file_exists($my_new_file))
        {
            // IF THE MOVE FUNCTION WORKED CORRECTLY
            if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
            {
                $upload_success = 1;
            }
            // IF THE MOVE FUNCTION FAILED
            else
            {
                $upload_success = -1;
            }
        }

        // IF THE FILE ALREADY EXISTS
        else
        {
            echo "<br/><b><i>$my_file</i></b> already exists.\n";

            // SHOULD WE OVERWRITE THE FILE? IF NOT
            if (empty($_POST["overwrite"]))
            {
                $upload_success = 0;
            }
            // IF WE SHOULD OVERWRITE THE FILE, TRY TO MAKE A BACKUP
            else
            {
                $now    = date('Y-m-d');
                $my_bak = $my_new_file . '.' . $now . '.bak';
                if (!copy($my_new_file, $my_bak))
                {
                    echo "<br/><strong>Attempted Backup Failed!</strong>\n";
                }
                if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
                {
                    $upload_success = 2;
                }
                else
                {
                    $upload_success = -1;
                }
            }
        }

        // REPORT OUR SUCCESS OR FAILURE
        if ($upload_success == 2) { echo "<br/>It has been overwritten.\n"; }
        if ($upload_success == 1) { echo "<br/><strong>$my_file</strong> has been saved.\n"; }
        if ($upload_success == 0) { echo "<br/><strong>It was NOT overwritten.</strong>\n"; }
        if ($upload_success < 0)  { echo "<br/><strong>ERROR: $my_file NOT SAVED - SEE WARNING FROM move_uploaded_file() COMMAND</strong>\n"; }
        if ($upload_success > 0)
        {
            echo "$file_size bytes uploaded.\n";
            chmod ($my_new_file, 0755);
            echo "<br/>chmod(0755) DONE: fileperms() = ";
            echo substr(sprintf('%o', fileperms($my_new_file)), -4);
            echo "<br/><a href=\"$my_file\">See the file $my_file</a>\n";
        }
    // END FOREACH ITERATOR - EACH ITERATION PROCESSES ONE FILE
    }
}

Open in new window

0
 
Sid_FCommented:
0
 
3XLcomAuthor Commented:
I tryed lots of upload example the problem is more then an upload problem. Please check out question carefully. If you create folder manually from filezilla it is working!! or if you make chown after php create folder via mkdir it works also !!! it only does not work when php create folder
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Ray PaseurCommented:
You might want to try using getcwd().

Also, I do not see any place that you make a new directory.  There is a function for that!
http://php.net/manual/en/function.mkdir.php
0
 
3XLcomAuthor Commented:
I think you did not check the complete code given :

$thisdir = getcwd();
try
{
echo 'bende';
mkdir($thisdir ."/deneme" , 0777 );
chmod($thisdir ."/deneme",0777);
chown($thisdir ."/deneme",xxxxx);
chmod($thisdir ."/deneme",0777);
0
 
Beverley PortlockCommented:
What is the difference in ownership between a folder created by PHP and a folder created by filezilla. The set up for PHP may not allow the chmod or chown commands to be effective. It all depends on how tight the server's security settings are.

Create two folders - one via PHP and one via FileZilla and look at the ownership and permissions. There must be a difference and it should be obvious what that difference is.


And no - I have not looked at any of the code posted  ;-)

0
 
Ray PaseurCommented:
OK, Now I see that you've got a mkdir() statement in the form script.  I would never have thought to look there; it would seem to belong in the action script.  You can access the form script via a GET request, and under the rules of HTTP a GET request must never change the data model.

On my Unix server, the target directories have permissions set to 0755.  This is the same settings for the files that I upload.  I'll try to create a working example for you.
0
 
Ray PaseurCommented:
Check this while I am trying to make and example for you.
http://us.php.net/manual/en/function.mkdir.php#105227
0
 
3XLcomAuthor Commented:
Not worked but folder created - RAY_junkasd chmod 755:

http://www.roportaj.com.tr/deneme/a.php


0
 
yuzhCommented:
Unfortunately most of the FTP clients have their own default permissions settings for file transfer, that's the cause of the problem.

eg:
User can set the default file permissions in filezilla ftp client as the following page,
http://buddingbloggers.com/2009/04/changing-file-permissions-via-filezilla/

or change the file/dir permissions in :
http://affiliate-minder.com/internetmarketing/changing-file-permissions-using-filezilla/

You can use cron to run a script to manually fix the permissions.

or make all your FTP users belong to the same groups, and the the dir permmisions to read and write, and
chmod g+s /ftp-dir.

please have a look at  my answer in http:Q_21097437.html
0
 
Daniel McAllisterPresident, IT4SOHO, LLCCommented:
The issue may also be in your PHP config... there is a setting in /etc/php.ini (assuming Linux here) that for many distributions needs to be reset

Check out the tutorials and notes HERE!

Good Luck

Dan
IT4SOHO
0
 
3XLcomAuthor Commented:
Thanks for all help problem was with safe mode
i closed safe mode and issue solved

king regards
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.