it should not be random, random != unique. or you can just use the user-id as prefix, then determine the file extension with strpos() and check if it is one of ['jpg', 'jpeg', 'png', 'gif']. your resulting filenamem ay look like $userid.$validextension
Alright, I am a bit stuck on how to get the $userid as the form doesn’t really get one and the id is coming from the database using AUTO_INCREMENT on each column so there is no PHP in the form that is asking for the user’s ID.
Here is my code so far to check to make sure the image extension of the file is valid:
// move uploaded file to directory (includes/uploads/avatar)
$upload_directory = 'includes/uploads/avatars';
// set file name (make it unique)
$file_name = '';
// sets $image
$image = $_FILES['avatar'][$file_name];
// finds extension of file uploaded...
$image_extension = strtolower(pathinfo($image,PATHINFO_EXTENSION));
// allowed extensions of file uploaded...
$valid_image_extensions = array('jpeg', 'jpg', 'png', 'gif');
// if image extension is a valid image extension...
if (inarray($image_extension, $valid_image_extensions)) {
// move the file to desired directory ($upload_directory)
move_uploaded_file($image, $upload_directory);
}
on an insert statement you first have to insert the user credentials to get a valid ID from auto_increment. after that use ::lastinsertid to get the ID and update the avatar afterwards with update
http://php.net/manual/en/pdo.lastinsertid.php
on an update you already may have the ID, because you have to know which row to update.
i don’t get how this should work as what you get from $_FILES then would be nothing. but the check afterwards ooks ok, just output all your variables to make sure you get what you want.
Would just $_FILES['avatar']; get something out of it and make the statement work since we aren’t actually using the tmp_name of the avatar anymore?
you already posted a dump of what you get from $_FILES so have a look at it and decide which information you need for which purpose - it provides the tmp_name for move_uploaded_file along with the name for checking the extension.
Yesterday I posted a var_dump($_FILES) and it displayed:
A:\wamp64\www\heartfx\register.php:154:
array (size=1)
'avatar' =>
array (size=5)
'name' => string '13124806_582930801867475_3656494793348492958_n.jpg' (length=50)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string 'A:\wamp64\tmp\php1BC.tmp' (length=24)
'error' => int 0
'size' => int 57127
So I want to get the name, type, & tmp_name of the image, so should $image look something like:
$image = $_FILES['avatar']['name']['type']['tmp_name']; ??
each of name, type, tmp_name is a single key, if you put them one after another you will go deeper into the array. first this is not possible with the data you have, and second you want each information seperately, so you have to start over at $_FILES for each information, just look at the documentation:
http://php.net/manual/en/features.file-upload.post-method.php
OK, wouldn’t that make it so $image_extension = $_FILES['avatar']['type'] work to get the extension of the image too instead of having to use strtolower…
Here is my code now so I have a variable for each one (name, type, tmp_name):
// move uploaded file to directory (includes/uploads/avatar)
$upload_directory = 'includes/uploads/avatars';
// set file name (make it unique)
$file_name = '';
// sets $image
$image = $_FILES['avatar'];
$image_name = $_FILES['avatar']['name'];
$image_tmp_name = $_FILES['avatar']['tmp_name'];
$image_type = $_FILES['avatar']['type'];
// finds extension of file uploaded...
$image_extension = strtolower(pathinfo($image,PATHINFO_EXTENSION));
// allowed extensions of file uploaded...
$valid_image_extensions = array('jpeg', 'jpg', 'png', 'gif');
// if image extension is a valid image extension...
if (inarray($image_extension, $valid_image_extensions)) {
// move the file to desired directory ($upload_directory)
move_uploaded_file($image, $upload_directory);
}
when you look at the var_dump you see that it contains the MIME type
'type' => string 'image/jpeg' (length=10)
but you can validate this by whitelist
$allowed_types = [
'image/jpeg' => 'jpg'
];
$type = $_FILES...;
if(isset($allowed_types[$type])){
$extension = $allowed_types[$type]
}
else {
die('unsupported file')
}
Oh, alright I understand because MIME type is 'image/jpeg' and supported files are 'JPEG'. I think I will just keep the original $image_extension then. Other then that, does my code currently look good as of right now with:
// move uploaded file to directory (includes/uploads/avatar)
$upload_directory = 'includes/uploads/avatars';
// sets $image & gets image type, name, tmp_name
$image = $_FILES['avatar'];
$image_name = $_FILES['avatar']['name'];
$image_tmp_name = $_FILES['avatar']['tmp_name'];
$image_type = $_FILES['avatar']['type'];
// set file name (make it unique)
$file_name = $activation_id$image_name;
// finds extension of file uploaded...
$image_extension = strtolower(pathinfo($image,PATHINFO_EXTENSION));
// allowed extensions of file uploaded...
$valid_image_extensions = array('jpeg', 'jpg', 'png', 'gif');
// if image extension is a valid image extension...
if (inarray($image_extension, $valid_image_extensions)) {
// move the file to desired directory ($upload_directory)
move_uploaded_file($image, $upload_directory);
}
have you ever tested your code? because alongside the parse-errors you will get, there is still some misconceptual stuff, at least in pathinfo($image, (array?) and move_uploaded_file($image, $upload_directory); (no filename?) where you do not use the right variables. Compare with the manual:
My apologies. So inested of using $image as the filename as manual displays an example of it should be something like this:
// move uploaded file to directory (includes/uploads/avatar)
$upload_directory = 'includes/uploads/avatars';
// sets $image & gets image type, name, tmp_name
$image = $_FILES['avatar'];
$image_name = $_FILES['avatar']['name'];
$image_tmp_name = $_FILES['avatar']['tmp_name'];
$image_type = $_FILES['avatar']['type'];
// set file name (make it unique)
$file_name = $activation_id.$image_name;
// finds extension of file uploaded...
$image_extension = strtolower(pathinfo($image,PATHINFO_EXTENSION));
// allowed extensions of file uploaded...
$valid_image_extensions = array('jpeg', 'jpg', 'png', 'gif');
// if image extension is a valid image extension...
if (inarray($image_extension, $valid_image_extensions)) {
// move the file to desired directory ($upload_directory)
move_uploaded_file($file_name, $upload_directory);
}
right?
for this i think you will get at least
- fatal: undefined character $
- fatal: string expected, array given
- and fatal: undefined function
I did get a few errors and adjusted code so it doesn’t display any errors after submitting form with this code:
// move uploaded file to directory (includes/uploads/avatar)
$upload_directory = '/includes/uploads/avatars';
// sets $image & gets image type, name, tmp_name
$image = $_FILES['avatar'];
$image_name = $_FILES['avatar']['name'];
$image_tmp_name = $_FILES['avatar']['tmp_name'];
$image_type = $_FILES['avatar']['type'];
// set file name (make it unique)
$file_name = $activation_id.$image_name;
// finds extension of file uploaded...
$image_extension = strtolower(pathinfo($_FILES['avatar']['name'],PATHINFO_EXTENSION));
// allowed extensions of file uploaded...
$valid_image_extensions = array('jpeg', 'jpg', 'png', 'gif');
// if image extension is a valid image extension...
if (in_array($image_extension, $valid_image_extensions)) {
// move the file to desired directory ($upload_directory)
move_uploaded_file($file_name, $upload_directory);
}
One issue I am having though is; It is not moving the file to the $upload_directory for some reason ![]()
so, what difference do you see to the code mentioned in the manual?
move_uploaded_file($tmp_name, "$uploads_dir/$name");
Sorry again
I was looking at the top of the page at the statement they provided:
bool move_uploaded_file ( string $filename , string $destination )
I have updated my code to:
// move uploaded file to directory (includes/uploads/avatar)
$upload_directory = 'includes/uploads/avatars';
// sets $image & gets image type, name, tmp_name
$image = $_FILES['avatar'];
$image_name = $_FILES['avatar']['name'];
$image_tmp_name = $_FILES['avatar']['tmp_name'];
$image_type = $_FILES['avatar']['type'];
// set file name (make it unique)
$file_name = $activation_id . $image_name;
// finds extension of file uploaded...
$image_extension = strtolower(pathinfo($_FILES['avatar']['name'],PATHINFO_EXTENSION));
// allowed extensions of file uploaded...
$valid_image_extensions = array('jpeg', 'jpg', 'png', 'gif');
// if image extension is a valid image extension...
if (in_array($image_extension, $valid_image_extensions)) {
// move the file to desired directory ($upload_directory)
move_uploaded_file($image_tmp_name, $upload_directory/$file_name);
}
```
I am not sure what is going wrong here. I have compared to code and only thing I feel like I can do different from the example provided at http://php.net/manual/en/function.move-uploaded-file.php is make `$file_name` a *basename* as they have it in the example?
i don’t think you want to devide something here, just have a look at the example above.
Awesome! Finally we got it to work an the images are successfully being put into the proper $upload_directory. Now I take it we need to store the $file_name in the database in a varchar so that we can grab it later on or will we be simply taking it from the directory without having to work with the database to get images?
try it!