[PHP] Comparing To File Names

Status
Not open for further replies.

JayC

Well-Known Member
Aug 8, 2013
5,505
1,401
I just use a simple text box right now and they type the file name:
<input type="text" name="filename" placeholder="Image"></text>

And I want to compare if the filename exists before adding it to the database so I don't get broken images

Directory I need them to check if the filename is there is:
*FileNameHere*?1111
 

Khalil

IDK
Dec 6, 2011
1,642
786
I'm not sure if I understood exactly what you're doing but..
PHP:
if(file_exists('linktodirectory/'.$_POST['filename'].'.extensionhere'))
{
// do something here if the file exists
}
 

JayC

Well-Known Member
Aug 8, 2013
5,505
1,401
I'm not sure if I understood exactly what you're doing but..
PHP:
if(file_exists('linktodirectory/'.$_POST['filename'].'.extensionhere'))
{
// do something here if the file exists
}
I thinkt hats what I need, how can I flip that so its if directory DOESN'T exist? Do i just put !( ) around it?
 

Khalil

IDK
Dec 6, 2011
1,642
786
PHP:
if(file_exists('linktodirectory/'.$_POST['filename'].'.extensionhere'))
{
// do something here if the file exists
} else {
// do something here if the file/directory does not exist
}


PHP:
if(file_exists('linktodirectory/'.$_POST['filename'].'.extensionhere'))
{
// do something here if the file exists
} elseif(!file_exists('linktodirectory/'.$_POST['filename'].'.extensionhere')) {
// do something here if the file/directory does not exist
}

Both work just fine.
 

JayC

Well-Known Member
Aug 8, 2013
5,505
1,401
Alright so this what I got now:

Thats the link its getting (I echo'd it out to debug)
That image DOES exist.

So its not grabbing the image or something?
Code:
if(isset($_POST['AddBadge']))
                                {
                                $badge_id = mysql_real_escape_string($_POST['addbadgeid']);
                                $cost = mysql_real_escape_string($_POST['badgecost']);
                                $rank = mysql_real_escape_string($_POST['badgerank']);
                                $CheckIfBadge = mysql_query("SELECT * FROM badgeshop WHERE badge_id='".$badge_id."'");
                                if($_SESSION['user']['rank'] > 9){
                                      if(mysql_num_rows($CheckIfBadge) > 0){
                                          echo "That badge is in the badgeshop";
                                      }elseif($cost > 50000 OR $cost < 100){
                                          echo "Cost must be reasonable, or add it from database";
                                      }elseif(!file_exists('https://habplex.org/game/c_images/album1584/'.$_POST['addbadgeid'].'.gif')){
                                          echo "Yay!";
                                          echo 'https://habplex.org/game/c_images/album1584/'.$_POST['addbadgeid'].'.gif?1111';
                                      }elseif($rank > 10 OR $rank < 1){
                                          echo "That rank is not supported for badges";
                                      }else{
                                          echo "<font color=green>Badge Added, Refresh Required</font>";
                                          //mysql_query("INSERT INTO badgeshop(badge_id,cost,rank) VALUES('".$badge_id."','".$cost."','".$rank."')");
                                          echo "How?";
                                      }
                                }else{
                                    echo "In order to edit badges you have to be rank 9";
                                }
                                }

whats its echoing:
Yay!

What I am entering:
PAT2 - Badge ID
200c - Cost
1 - Rank
 

Khalil

IDK
Dec 6, 2011
1,642
786
PHP:
if(isset($_POST['AddBadge']))
{
    $Badge = array(
        'ID' => mysql_real_escape_string($_POST['addbadgeid']),
        'Cost' => mysql_real_escape_string($_POST['badgecost']),
        'Rank' => mysql_real_escape_string($_POST['badgerank'])
    );

    $gQuery = mysql_query("SELECT * FROM `badgeshop` WHERE `badge_id` = '".$Badge['ID']."'");

    if($_SESSION['user']['rank'] >= 9)
    {
        if(mysql_num_rows($gQuery) < 1)
        {
            if($Badge['Cost'] < 50000 && $Badge['Cost'] > 100)
            {
                if(file_exists('https://habplex.org/game/c_images/album1584/'.$Badge['ID'].'.gif?1111'))
                {
                    if($Badge['Rank'] < 10 && $Badge['Rank'] > 1)
                    {
                        if(mysql_query("INSERT INTO `badgeshop` (`badge_id`, `cost`, `rank`) VALUES ('".$Badge['ID']."', '".$Badge['Cost']."', '".$Badge['Rank']."'"))
                        {
                            echo '<font color=\'green\'>Badge added, refresh required</font>';
                        }
                    } else {
                        echo 'That rank is not supported for badges.';
                    }
                } else {
                    echo 'This badge does not exist at all';
                }
            } else {
                echo 'Price must be reasonable';
            }
        } else {
            echo 'The badge is already in the badgeshop';
        }
    } else {
        echo 'In order to edit badges, you need to be rank 9 or greater';
    }
}
 

brsy

nah mang
May 12, 2011
1,530
272
You can't use file_exists with a remote URL. You need to use the local image path.

PHP:
<?php

if(isset($_POST['AddBadge'])) {
    $badge = array(
        'id' => mysql_real_escape_string($_POST['addbadgeid']),
        'cost' => mysql_real_escape_string($_POST['badgecost']),
        'rank' => mysql_real_escape_string($_POST['badgerank'])
    );

    $gQuery = mysql_query("SELECT * FROM `badgeshop` WHERE `badge_id` = '".$badge['id']."'");

    if($_SESSION['user']['rank'] >= 9) {
        if(mysql_num_rows($gQuery) == 0) {
            if($badge['cost'] < 50000 && $badge['cost'] > 100) {
                if(fopen('https://habplex.org/game/c_images/album1584/'.$badge['id'].'.gif?1111', 'r')) {
                    if($badge['rank'] < 10 && $badge['rank'] > 1) {
                        if(mysql_query("INSERT INTO `badgeshop` (`badge_id`, `cost`, `rank`) VALUES ('" . $badge['id'] . "', '" . $badge['cost'] . "', '" . $badge['rank'] . "'")) {
                            echo '<font color=\'green\'>Badge added, refresh required</font>';
                        } else {
                            die("Failed to run query");
                        }
                    } else {
                        echo 'That rank is not supported for badges.';
                    }
                } else {
                    echo 'This badge does not exist at all';
                }
            } else {
                echo 'Price must be reasonable';
            }
        } else {
            echo 'The badge is already in the badgeshop';
        }
    } else {
        echo 'In order to edit badges, you need to be rank 9 or greater';
    }
}

?>
 
Last edited:
Status
Not open for further replies.

Users who are viewing this thread

Top