tags:

views:

144

answers:

4

Hi, I have this code I been working on but I'm having a hard time for it to work. I did one but it only works in php 5.3 and I realized my host only supports php 5.0! do I was trying to see if I could get it to work on my sever correctly, I'm just lost and tired lol

Ol, sorry stackoverflow is a new thing for me. Not sure how to think of it. As a forum or a place to post a question... hmmm, I'm sorry for being rude with my method of asking.

I was wondering i you could give me some guidance on how to properly insert directory structures with how i written this code. I wasn't sure how to tell the PHP where to upload my files and whatnot, I got some help from a friend who helped me sort out some of my bugs, but I'm still lost with dealing with the mkdir and link, unlink functions. Is this how I am suppose to refer to my diretories?

I know php 5.3 uses the _ DIR _ and php 5.0 use dirname(_ _ FILE_ _), I have tried both and I get the same errors. My files are set to 0777 for testing purposes. What could be the problem with it now wanting to write and move my uploaded file?

    } elseif ( (file_exists("\\uploads\\{$username}\\images\\banner\\{$filename}")) || (file_exists("\\uploads\\{$username}\\images\\banner\\thumbs\\{$filename}")) ) {

        $errors['img_fileexists'] = true;
    }

    if (! empty($errors)) { 
        unlink($_FILES[IMG_FIELD_NAME]['tmp_name']); //cleanup: delete temp file
    }

    // Create thumbnail
    if (empty($errors)) {

        // Make directory if it doesn't exist
        if (!is_dir("\\uploads\\{$username}\\images\\banner\\thumbs\\")) {

            // Take directory and break it down into folders
            $dir = "uploads\\{$username}\\images\\banner\\thumbs";
            $folders = explode("\\", $dir);

            // Create directory, adding folders as necessary as we go (ignore mkdir() errors, we'll check existance of full dir in a sec)
            $dirTmp = '';
            foreach ($folders as $fldr) {
                if ($dirTmp != '') { $dirTmp .= "\\"; }
                $dirTmp .= $fldr;
                mkdir("\\".$dirTmp); //ignoring errors deliberately!
            }

            // Check again whether it exists
            if (!is_dir("\\uploads\\$username\\images\\banner\\thumbs\\")) {
                $errors['move_source'] = true;
                unlink($_FILES[IMG_FIELD_NAME]['tmp_name']); //cleanup: delete temp file
            }
        }

        if (empty($errors)) {

            // Move uploaded file to final destination
            if (! move_uploaded_file($_FILES[IMG_FIELD_NAME]['tmp_name'], "/uploads/$username/images/banner/$filename")) {
                $errors['move_source'] = true;
                unlink($_FILES[IMG_FIELD_NAME]['tmp_name']); //cleanup: delete temp file

            } else {

                // Create thumbnail in new dir
                if (! make_thumb("/uploads/$username/images/banner/$filename", "/uploads/$username/images/banner/thumbs/$filename")) {
                    $errors['thumb'] = true;
                    unlink("/uploads/$username/images/banner/$filename"); //cleanup: delete source file
                }
            }
        }
    }

    // Record in database
    if (empty($errors)) {

        // Find existing record and delete existing images
        $sql = "SELECT `bannerORIGINAL`, `bannerTHUMB` FROM `agent_settings` WHERE (`agent_id`={$user_id}) LIMIT 1";
        $result = mysql_query($sql);
        if (!$result) {
            unlink("/uploads/$username/images/banner/$filename"); //cleanup: delete source file
            unlink("/uploads/$username/images/banner/thumbs/$filename"); //cleanup: delete thumbnail file
            die("<div><b>Error: Problem occurred with Database Query!</b><br /><br /><b>File:</b> " . __FILE__ . "<br /><b>Line:</b> " . __LINE__ . "<br /><b>MySQL Error Num:</b> " . mysql_errno() . "<br /><b>MySQL Error:</b> " . mysql_error() . "</div>");
        }
        $numResults = mysql_num_rows($result);
        if ($numResults == 1) {
            $row = mysql_fetch_assoc($result);

            // Delete old files
            unlink("/uploads/$username/images/banner/" . $row['bannerORIGINAL']); //delete OLD source file
            unlink("/uploads/$username/images/banner/thumbs/" . $row['bannerTHUMB']); //delete OLD thumbnail file
        }

        // Update/create record with new images
        if ($numResults == 1) {
            $sql = "INSERT INTO `agent_settings` (`agent_id`, `bannerORIGINAL`, `bannerTHUMB`) VALUES ({$user_id}, '/uploads/$username/images/banner/$filename', '/uploads/$username/images/banner/thumbs/$filename')";
        } else {
            $sql = "UPDATE `agent_settings` SET `bannerORIGINAL`='/uploads/$username/images/banner/$filename', `bannerTHUMB`='/uploads/$username/images/banner/thumbs/$filename' WHERE (`agent_id`={$user_id})";
        }
        $result = mysql_query($sql);
        if (!$result) {
            unlink("/uploads/$username/images/banner/$filename"); //cleanup: delete source file
            unlink("/uploads/$username/images/banner/thumbs/$filename"); //cleanup: delete thumbnail file
            die("<div><b>Error: Problem occurred with Database Query!</b><br /><br /><b>File:</b> " . __FILE__ . "<br /><b>Line:</b> " . __LINE__ . "<br /><b>MySQL Error Num:</b> " . mysql_errno() . "<br /><b>MySQL Error:</b> " . mysql_error() . "</div>");
        }
    }

    // Print success message and how the thumbnail image created
    if (empty($errors)) {
        echo "<p>Thumbnail created Successfully!</p>\n";
        echo "<img src=\"/uploads/$username/images/banner/thumbs/$filename\" alt=\"New image thumbnail\" />\n";
        echo "<br />\n";
    }
}

I get the following errors:

Warning: move_uploaded_file(./uploads/saiyanz2k/images/banner/azumanga-wall.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php on line 112 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/services/webdata/phpupload/phpVoIEQj' to './uploads/saiyanz2k/images/banner/azumanga-wall.jpg' in /services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php on line 112

A: 

One way is to check from within your code whether a certain command/function is available for use. You can use the function_exists function for that eg:

if (function_exists('date_default_timezone_set'))
{
  date_default_timezone_set("GMT");
}
else
{
  echo 'date_default_timezone_set is not supported....';
}
Sarfraz
well, it works perfect if i use 5.3 and __DIR__ but since Im on 5.0 i getWarning: move_uploaded_file(./uploads/saiyanz2k/images/banner/azumanga-wall.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php on line 112Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/services/webdata/phpupload/phpVoIEQj' to './uploads/saiyanz2k/images/banner/azumanga-wall.jpg' in /services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php on line 112
s2xi
@user267490: if that is the error, it does not mean a features does not work, it means most likely that the path of the uploaded file is wrong or the upload limit is set very low in php.ini file.
Sarfraz
hmmm, ya i think that my directory structure isn't being done correct. Perhaps this is what I'm really having a hard time with and not the code. um, my dir on my server is /uploads/$username/images/banner/ that i'm trying to work with.
s2xi
@@user267490: make sure that you have write access to the directoy, chmod it to 755 or 777
Sarfraz
i have all folders I mentioned in my code set to 0777 to ensure that it will work. I have edited my original post to better describe the issue
s2xi
@user267490: generally speaking, it's impossible to tell which line is a given line in a script fragment. Moreover, we shouldn't have to count lines. If an error gives a line number, indicate which line it is in the source with a comment (e.g. put `// line 112` at the end of line 112). Also, the error message should go in the question (see edit).
outis
A: 

Ahh! I'm sorry, didn't mean to vent my frustration on you guys. But I have been at this for hours now it seems.

Like i mentioned this code works but since my server is picky I can't user the 5.3 syntax I coded. This is my attempt to make it work on the 5.0 php my server has.

In particular I think there is something wrong with the mkdir() and the unlink() functions.

if you go to www.helixagent.com log in with test/test then in the url go to /upload2.php then you will see the errors its throwing at me.

well, it works perfect if i use 5.3 and DIR but since I'm on 5.0 i tried a different method

the errors i get are

Warning: move_uploaded_file(./uploads/saiyanz2k/images/banner/azumanga-wall.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php on line 112

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/services/webdata/phpupload/phpVoIEQj' to './uploads/saiyanz2k/images/banner/azumanga-wall.jpg' in /services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php on line 112
s2xi
The equivalent to `__DIR__` in PHP < 5.3 is `dirname(__FILE__);` - The errors suggest your destination directory is not writable, which is not a PHP issue. Also, please provide additional information by editing your question. SO is not used like a forum.
Gordon
outis
A: 

It looks like you don't have access to the folder (or file)

/uploads/$username/images/banner/$filename

which could be because of a basedir restriction on the host (e.g. you may not leve the parent directory /services/webdata/) or just a missing permission in the os.

Try to (temporary) set permission of /uploads/ to 777 or execute the script from console to see if you have a basedir restriction.

SchlaWiener
A: 

Take a closer look at the paths in the error messages:

./uploads/saiyanz2k/images/banner/azumanga-wall.jpg
/services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php

The destination is a relative path, most likely relative to upload2.php's directory. The one relative path I see is the line:

        // Take directory and break it down into folders
        $dir = "uploads\\{$username}\\images\\banner\\thumbs";

Which should probably be:

        // Take directory and break it down into folders
        $dir = "\\uploads\\{$username}\\images\\banner\\thumbs";

Actually, it should be

        $dir = "/uploads/{$username}/images/banner/thumbs";

since PHP supports using a forward slash as directory separator on all platforms, while the backslash is only supported on MS platforms.

outis