Jump to content

image uploaded always has a capital letter


npsari

Recommended Posts

Hi everyone, I have a script below, which uplads an image, however, the image name always starts with a capital letter, I want all letters to be small, how to adjust this please, thank you

 

$target_dir = "";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$target_file = "$get_current_user.jpg";


$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));


// Check if image file is a actual image

if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo ""; /// was File is an image
        $uploadOk = 1;
    } else {
        echo "File is not an image<br>";
        $uploadOk = 0;
    }
}


// Check file size

if ($_FILES["fileToUpload"]["size"] > 10000000) {
    echo "Sorry, this image is too large, please resize using Paint<br>";
    $uploadOk = 0;
}

// Allow certain file formats

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    echo "Only JPG, JPEG, PNG & GIF files are allowed<br>";
    $uploadOk = 0;
}


// Check if $uploadOk is set to 0 by an error

if ($uploadOk == 0) {

    echo "There was an error<br>";


} else {


    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

       echo "Uploaded successfully (You may need to clear Cache to see the new image)";
	   mysql_query("UPDATE user SET user_image = 'https://.../images/users/$get_current_user.jpg' WHERE user_name = '$get_current_user' ");

    } else {

        echo ""; /// was Select a suitable image, file not uploaded yet

    }

}

 

Link to comment
Share on other sites

Thank you, but where do I put this strtolower function? As you can see from the code above, it is already there, but it is doing nothing, because the first letter of the image name still becomes capital letter for some reason, even the $get_current_user is all lower case, so something in this code is turning the first letter of the image from a small letter into a capital letter

Link to comment
Share on other sites

My knowledge of $_FILES is a bit rusty, but I'm confused by the following lines:

$target_dir = "";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$target_file = "$get_current_user.jpg";

Based on the PHP manual, $_FILES["fileToUpload"]["name"] should give you the name of the uploaded file. That's where you would apply the strtolower() function. Note that $_FILES["fileToUpload"]["name"] should only give you the file name. You don't need to run the file name through the basename() function since there is not path to process.

Also note that you are overwriting $target_file in the next line with "$get_current_user.jpg". So basically you no longer have the name of the uploaded file. I'm also not seeing where $get_current_user is defined. So I'm not really sure where you're getting a file name with an upper case letter. ?

Link to comment
Share on other sites

Before I get into this, PHP has a Boolean type.  You should use it and have code like this:

 

//Set initialization to success, and test this assumption
$uploadOk = true;

if (some_test_that_fails) {
    ....
    $uploadOk = false;
}

if (some_other_test_that_fails) {
    ......
    $uploadOk = false;
}

if ($uploadOk) {
   // Everything is ok, proceed etc.
} else {
   // It failed
}

 

In your case, since you already wrote the code with the "false" block first, you can simply test for "Not $uploadOk" which is:

if (!$uploadOk) {
  // Handle Error...
} else {
  // Proceed
}

 

 

There are a couple of issues with your code I see:

$target_file = "$get_current_user.jpg";

You do not include this, but it implies that a variable: $get_current_user is set in some code you didn't reference or describe.    Since we don't know what that variable contains, at very least we can see that when the image file is eventually saved it will be named  'Whatever.jpg'.

So this is the crux of your issue... if the username is 'Someuser', then the file will be named 'Someuser.jpg'.  Later on, your system expects this apparently.  Because you do this query:

mysql_query("UPDATE user SET user_image = 'https://.../images/users/$get_current_user.jpg' WHERE user_name = '$get_current_user' ");

3 Things here:

  1. Set the path to be relative to the document root ie.   '/images/users/....' rather than using a full url.  If you ever move this code, you will regret that you hardcoded your domain
  2. mysql_query is long gone from php.  You should be using either mysqli or PDO.  Consensus is that PDO is a better api.  You should also not be using interpolation for your parameters, but instead be using Prepared statements and binding the variables to the query.  See the PDO Tutorial .
  3. The problem with your file naming is that no matter what type of image you have, you make the extension .jpg.  

So basically, your issue can be addressed by making a few changes:

Change the initialization from what I quoted above to:

$target_file = strtolower($get_current_user);

Notice no extension forced here.

You will set the finalized $target_file variable only when you need it:

if (!$uploadOk) {
    echo "There was an error<br>";
} else {
    // Tack on the extension
    $target_file .= '.' . $imageFileType;
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

       echo "Uploaded successfully (You may need to clear Cache to see the new image)";
       // Again this needs to be PDO or Mysqli code
	   mysql_query("UPDATE user SET user_image = CONCAT('/images/users/, $target_file) WHERE user_name = '$get_current_user'");
    } else {
        echo ""; /// was Select a suitable image, file not uploaded yet
    }

}

 

Last but not least, this assumes that the files are getting stored in the right directory, since you don't specify a path to the target for move_uploaded_file.  That needs to be the real file system directory path where the images will be stored.

Link to comment
Share on other sites

Thank you guys, thank you Gizmola

So php is changing, I don't have time to learn this new mysqli or PDO :(

Is it wrong saving all images as .jpg? It makes life little easier for me

Yes $get_current_user is always small letters, because when the user creates an account, their username is converted to lower case first, then saved in the mySQL database, I really wonder how does it become capital letter later, i need to check my codes again, to make sure $get_current_user is always small letters

Edited by npsari
Link to comment
Share on other sites

22 minutes ago, npsari said:

So php is changing, I don't have time to learn this new mysqli or PDO :(

 

PHP HAS changed.  The switch away from the mysql interface began several/many years ago.  You're just a bit late to the party.  And as chhorn says your script will cease to work once your provider removes it from his PHP install.  The only reason it works for you now is because your provider is not moving quite yet, but sooner or later he will and then you'll be stuck.

Of course if you are running your own db server and managing your own php installation, go right ahead living in the past.  Eventually you will have to move on and then you will have to modify all of your programs.  Will you then have the time to not only learn PDO but to make all those coding changes?  I think not.

Since your knowledge seems limited at this point anyway, why not spend a little time to pick up using PDO?  It's a small investment in your skill set that will last a long time.  Look at the manual for many good examples of how to make a connection, how to use prepared queries and their arguments and how to execute the query and fetch the results.  It's really not much different than the mysql extension - just a few different concepts.

And as for "always using jpg".  If the file, ie, the image, is NOT a jpg file you cannot simply call it one since you may not be able to open it later on.  What you need to do with proper file upload logic is to check what the file type is when it is being uploaded and decide if it is one of your expected types before accepting it and storing it in your picture repository.  You don't just decide to name it as a ".jpg" file.  

Link to comment
Share on other sites

thank you ginerjem

yes, php is my hobby, nothing serious, just making websites for fun or little money

uploading images is the most annoying code on php, for the past 10 years, i never understoud this part, or never found a code online which does everything

this code I am using above, i found online also, but it does not resize images, and it seems to have several issues

i wonder how websites like eBay have such sophosticated image uploading tools, they must be really PHP gurus

 

Link to comment
Share on other sites

"just making websites for fun or little money"???  

You'll have to upgrade your skill set to continue doing this hobby.  

Of course there are difficult things but what you are currently trying to accomplish is not that hard to do.  You just have to organize your thoughts and write the correct code.  So far from what you are asking that part is the hard thing for you to do currently.  Perhaps you need to do some reading since you don't have a handle on the basics of files if you think that you can simply re-name an image file because you like a specific name.

Once you manage to write a satisfactory (to you) upload module, you could then read up on the functions available (see the official PHP manual) to do re-sizing.  Again - you will have to LEARN things.  BTW - if this is just a hobby, why don't you have the time to learn about these things?  Isn't part of a hobby the fact that you actually learn from it?  It can't be much fun if it's always work because you haven't managed to figure things out.

 

PS - have we managed to solve your original problem with the case of your filename?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.