Page 1 of 1

Check if string is empty

Posted: Wed Dec 10, 2008 11:01 am
by TheBrandon
Hello everyone,

I'm trying to upload an image and if no image is uploaded, I want it to default to another image.

My string keeps coming out like this though, and I can't figure out how to do an if/else on it so I can apply it properly.

Code: Select all

string(0) ""
Here's some of the PHP I'm using now:

Code: Select all

$image = str_replace(" ", "", $_POST['uploadfile']); //Image
 
var_dump($image);
 
$str = strlen($image);
 
if(!$str=="0"){echo "0.";}else{echo "more than 0.";}
exit;
 
if(empty($image)){
 
}else{
$filename = "../images/picture-na.jpg";
}
 
I've also tried these:

Code: Select all

if($image[0] == ""){
    echo "wooha!";
    }else{echo "oh my!";}
$image=trim($image);
echo $image;

Code: Select all

if(empty($image)){

Code: Select all

if(!empty($image)){

Code: Select all

if(!trim($image) == ''){
Any help would be appreciated. As you can see, I've tried to troubleshoot this myself but I just seem to be missing something.

My normal str_replace on it is this btw:

Code: Select all

$image = str_replace("-", ".", $_POST['uploadfile']); //Image
I switched it in the above example when I was troubleshooting.

Thanks for any help!

Re: Check if string is empty

Posted: Wed Dec 10, 2008 1:00 pm
by TheBrandon
If I use this:

Code: Select all

if(strlen($image)<2){
 
// Image Handling
// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
 
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);
 
// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);
 
// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=177;
$newheight=($height/$width)*177;
$tmp=imagecreatetruecolor($newwidth,$newheight);
 
// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
 
// now write the resized image to disk. The upload path is the first part of $filename
$filename = "../images/uploaded/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);
 
// kill the temp files
imagedestroy($src);
imagedestroy($tmp);
 
}else{
$filename = "../images/picture-na.jpg";
}
Then I can get the new file to display, but if I don't upload anything I just get errors, so it's not defaulting to the blank image.

These are the errors I get:

Code: Select all

Warning: Division by zero in /home/content/d/a/n/dandd2008/html/admin/submitupdate.php on line 47
 
Warning: imagecreatetruecolor(): Invalid image dimensions in /home/content/d/a/n/dandd2008/html/admin/submitupdate.php on line 48
 
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/content/d/a/n/dandd2008/html/admin/submitupdate.php on line 52
 
Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/content/d/a/n/dandd2008/html/admin/submitupdate.php on line 56
 
Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/content/d/a/n/dandd2008/html/admin/submitupdate.php on line 59
 
Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/content/d/a/n/dandd2008/html/admin/submitupdate.php on line 60
Which are caused because I didn't upload an image, but it's still executing them instead of skipping them.

If I change

Code: Select all

if(strlen($image)<2){
to

Code: Select all

if(strlen($image)>2){
It will ONLY display the "no image found" image.

This is the full script:

Code: Select all

<?php
require_once('global.php');
$cookie = $_COOKIE['Cookie'];
if ($cookie = "true"){
 
//get ID from URL
$EDITid = $_POST['EDITid'];
 
// Data Handling
$date = str_replace("-", ".", $_POST['date']); //Date
$street = str_replace("-", ".", $_POST['street']); //Street
$city = str_replace("-", ".", $_POST['city']); //City
$bedrooms = str_replace("-", ".", $_POST['bedrooms']); //Bedrooms
$baths = str_replace("-", ".", $_POST['baths']); //Baths
$parking = str_replace("-", ".", $_POST['parking']); //Parking
$movein = str_replace("-", ".", $_POST['movein']); //Move In Date
$price = str_replace("-", ".", $_POST['price']); //Price
$security = str_replace("-", ".", $_POST['security']); //Security Deposit
$pets = str_replace("-", ".", $_POST['pets']); //Pets
$petfee = str_replace("-", ".", $_POST['petfee']); //Pet Fee
$sqfeet = str_replace("-", ".", $_POST['sqfeet']); //Square Feet
$year = str_replace("-", ".", $_POST['year']); //Year Constructed
$image = str_replace(" ", "", $_POST['uploadfile']); //Image
$status = str_replace("-", ".", $_POST['status']); //Status
$sign = str_replace("-", ".", $_POST['sign']); //Sign Displayed
$hometype = str_replace("-", ".", $_POST['hometype']); //Type of Home
 
if(strlen($image)<2){
 
// Image Handling
// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
 
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);
 
// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);
 
// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=177;
$newheight=($height/$width)*177;
$tmp=imagecreatetruecolor($newwidth,$newheight);
 
// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
 
// now write the resized image to disk. The upload path is the first part of $filename
$filename = "../images/uploaded/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);
 
// kill the temp files
imagedestroy($src);
imagedestroy($tmp);
 
}else{
$filename = "../images/picture-na.jpg";
}
// connect to the database
db_connect();
 
$query = "UPDATE units SET date='$date', street='$street', city='$city', bedrooms='$bedrooms', baths='$baths', parking='$parking', movein='$movein', price='$price', security='$security', pets='$pets', petfee='$petfee', sqfeet='$sqfeet', year='$year', image='$filename', status='$status', sign='$sign', type='$hometype' WHERE id='$EDITid'";
 
//check for errors
$results = mysql_query($query) or die ("Could not execute query : $query." . mysql_error());
if ($results){
//success! Echo the $filename variable to access the new image!
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head profile="http://www.w3.org/2000/08/w3c-synd/#"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Revive Media Services" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>D and D Admin</title>
<link rel="stylesheet" type="text/css" href="../css/reset.css" />
<link rel="stylesheet" type="text/css" href="../css/style.css" />
</head>
        <body>
                <div id="containeradmin">
            <div id="adminmenu">
                <ul id="adminnav">
                    <li><a href="index-true.php">Admin Home</a></li>
                    <li><a href="add.php">Add New Location</a></li>
                </ul><div class="clear"></div>
            </div>
    <div id="container">
        <h1>Data Submitted.</h1>
    </div>
    </div>
</body>
</html>
<?
}
}else{echo "fail";}
?>
 

Re: Check if string is empty

Posted: Wed Dec 10, 2008 2:02 pm
by requinix
$_POST[uploadfile] doesn't exist.

Read up on how to handle file uploads.

Re: Check if string is empty

Posted: Wed Dec 10, 2008 2:04 pm
by TheBrandon
tasairis wrote:$_POST[uploadfile] doesn't exist.

Read up on how to handle file uploads.

Code: Select all

<label for="image">Image:</label><input type="file" name="uploadfile" class="rightinput"/>
It's uploading, resizing, and moving them fine.

It's just that I can't say "if there is data, do this. Otherwise, do this" in this situation.

Re: Check if string is empty

Posted: Wed Dec 10, 2008 2:12 pm
by requinix
TheBrandon wrote:It's just that I can't say "if there is data, do this. Otherwise, do this" in this situation.
Right. The problem is that $_POST[uploadfile] doesn't exist, which you would probably discover if you refresh your memory on how to handle file uploads.

Since $image depends on a variable that doesn't exist, strlen($image) won't work like you think it will and your code won't work either.

Re: Check if string is empty

Posted: Wed Dec 10, 2008 2:18 pm
by desperado
do you mean this?

if (file_exists($filename)) {
echo "blah";
} else {
echo "blah";
}

Re: Check if string is empty

Posted: Wed Dec 10, 2008 2:23 pm
by TheBrandon
tasairis wrote:
TheBrandon wrote:It's just that I can't say "if there is data, do this. Otherwise, do this" in this situation.
Right. The problem is that $_POST[uploadfile] doesn't exist, which you would probably discover if you refresh your memory on how to handle file uploads.

Since $image depends on a variable that doesn't exist, strlen($image) won't work like you think it will and your code won't work either.
Crap you're right. I forgot about that $_FILES global.

It always sucks when you've made like 100 scripts using something then have that lapse in memory/judgement.

Thanks.

Should I just see if the global has no size? Do you think that would be the best thing to test with this situation? Like "if the $_FILES size = 0, use the placeholder pic, otherwise use the uploaded pic"

Re: Check if string is empty

Posted: Wed Dec 10, 2008 4:20 pm
by TheBrandon
K, what am I missing now?

Code: Select all

<?php
$cookie = $_COOKIE['sdfsdf'];
if ($cookie = "true"){
 
//get ID from URL
$EDITid = $_POST['EDITid'];
 
// Data Handling
$date = str_replace("-", ".", $_POST['date']); //Date
$street = str_replace("-", ".", $_POST['street']); //Street
$city = str_replace("-", ".", $_POST['city']); //City
$bedrooms = str_replace("-", ".", $_POST['bedrooms']); //Bedrooms
$baths = str_replace("-", ".", $_POST['baths']); //Baths
$parking = str_replace("-", ".", $_POST['parking']); //Parking
$movein = str_replace("-", ".", $_POST['movein']); //Move In Date
$price = str_replace("-", ".", $_POST['price']); //Price
$security = str_replace("-", ".", $_POST['security']); //Security Deposit
$pets = str_replace("-", ".", $_POST['pets']); //Pets
$petfee = str_replace("-", ".", $_POST['petfee']); //Pet Fee
$sqfeet = str_replace("-", ".", $_POST['sqfeet']); //Square Feet
$year = str_replace("-", ".", $_POST['year']); //Year Constructed
$image = str_replace(" ", "", $_POST['uploadfile']); //Image
$status = str_replace("-", ".", $_POST['status']); //Status
$sign = str_replace("-", ".", $_POST['sign']); //Sign Displayed
$hometype = str_replace("-", ".", $_POST['hometype']); //Type of Home
 
$filesize = $_FILES['uploadfile']['size'];
 
if($filesize=="0"){
// Image Handling
// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
 
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);
 
// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);
 
// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=177;
$newheight=($height/$width)*177;
$tmp=imagecreatetruecolor($newwidth,$newheight);
 
// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
 
// now write the resized image to disk. The upload path is the first part of $filename
$filename = "../images/uploaded/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);
 
// kill the temp files
imagedestroy($src);
imagedestroy($tmp);
 
}else{
$filename = "../images/picture-na.jpg";
}
// connect to the database
db_connect();
 
$query = "UPDATE units SET date='$date', street='$street', city='$city', bedrooms='$bedrooms', baths='$baths', parking='$parking', movein='$movein', price='$price', security='$security', pets='$pets', petfee='$petfee', sqfeet='$sqfeet', year='$year', image='$filename', status='$status', sign='$sign', type='$hometype' WHERE id='$EDITid'";
 
//check for errors
$results = mysql_query($query) or die ("Could not execute query : $query." . mysql_error());
if ($results){
//success! Echo the $filename variable to access the new image!
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head profile="http://www.w3.org/2000/08/w3c-synd/#"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Revive Media Services" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>D and D Admin</title>
<link rel="stylesheet" type="text/css" href="../css/reset.css" />
<link rel="stylesheet" type="text/css" href="../css/style.css" />
</head>
        <body>
                <div id="containeradmin">
            <div id="adminmenu">
                <ul id="adminnav">
                    <li><a href="index-true.php">Admin Home</a></li>
                    <li><a href="add.php">Add New Location</a></li>
                </ul><div class="clear"></div>
            </div>
    <div id="container">
        <h1>Data Submitted.</h1>
    </div>
    </div>
</body>
</html>
<?
}
}else{echo "fail";}
?>
It always returns the image placeholder. :(

Re: Check if string is empty

Posted: Wed Dec 10, 2008 5:04 pm
by requinix
If the file's size is zero then process the image?