Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

mkdir and upload problem - php

Posted on 2011-10-14
14
Medium Priority
?
558 Views
Last Modified: 2012-05-12
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 ?
0
Comment
Question by:3XLcom
12 Comments
 
LVL 6

Expert Comment

by:Sid_F
ID: 36970344
0
 

Author Comment

by:3XLcom
ID: 36970379
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36971360
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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 

Author Comment

by:3XLcom
ID: 36971481
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
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 36971680
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36973120
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36973152
Check this while I am trying to make and example for you.
http://us.php.net/manual/en/function.mkdir.php#105227
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 668 total points
ID: 36973179
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
 

Author Comment

by:3XLcom
ID: 36973594
Not worked but folder created - RAY_junkasd chmod 755:

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


0
 
LVL 38

Assisted Solution

by:yuzh
yuzh earned 664 total points
ID: 36979048
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
 
LVL 21

Assisted Solution

by:Daniel McAllister
Daniel McAllister earned 668 total points
ID: 36979248
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
 

Author Closing Comment

by:3XLcom
ID: 36979264
Thanks for all help problem was with safe mode
i closed safe mode and issue solved

king regards
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

576 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question