I'm trying to allow users to upload images as a profile picture to my site. I currently have an html form which is linked to my PHP code. I would like for the following to occur:
file name to be changed to the UserID.Username.ext where the userID and the Username are Razor var's on the page containing the html form
file to replace any existing file with the same name in the directory
file to be limited to .jpg,.jpeg,.png,.gif and to size of 300000
new file path to be stored in sql database based on the userID and Username mentioned above
I have no experience with PHP and have tried researching these areas. I have found good sources for each, but I do not know how to format and adjust the PHP to work correctly with all the criteria.
My basic upload PHP file is as follows:
$fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);
if ($fn) {
// AJAX call
file_put_contents(
'uploads/' . $fn,
file_get_contents('php://input')
);
echo "$fn uploaded";
exit();
}
else {
// form submit
$files = $_FILES['fileselect'];
foreach ($files['error'] as $id => $err) {
if ($err == UPLOAD_ERR_OK) {
$fn = $files['name'][$id];
move_uploaded_file(
$files['tmp_name'][$id],
'uploads/' . $fn
);
echo "<p>File $fn uploaded.</p>";}}}
I have found the following codes for the respective areas above:
1 and 3: Rename uploaded files
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$filesize = $_FILES["file"]["size"];
$allowed_file_types = array('.jpg','.jpeg','.png','.gif');
if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000))
{
// Rename file
$newfilename = "$imagecounter-".$file_basename . $file_ext;
if (file_exists("uploads/".$date."-".$email."-vid-".$videoID."/" . $newfilename))
PHP File upload and overwrite file with same name
if(file_exists("documenti/$fileName")) unlink("documenti/$fileName");
How to store file name in database, with other info while uploading image to server using PHP?
// Connects to your Database mysql_connect("yourhost", "username", "password") or die(mysql_error()) ; mysql_select_db("dbName") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO tableName (nameMember,bandMember,photo,aboutMember,otherBands) VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;
So, I've found all the necessary baseline codes to meet my desired criteria. I just don't know the proper PHP syntax to use to throw it all together.
Thanks for the help and advice!