0

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:

  1. 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

  2. file to replace any existing file with the same name in the directory

  3. file to be limited to .jpg,.jpeg,.png,.gif and to size of 300000

  4. 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))
  1. PHP File upload and overwrite file with same name

    if(file_exists("documenti/$fileName")) 
    unlink("documenti/$fileName");
    
  2. 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!

Community
  • 1
  • 1
user44129
  • 71
  • 1
  • 7

2 Answers2

0

when uploading a file you can change the name right there, example from your code,

move_uploaded_file(
            $files['tmp_name'][$id],
            'uploads/' . $fn
);

the format is move_uploaded_files($oldname, $newname) so you can name it anything and attach the file extension to it.

I really recommend using a framework such as codeigniter or laravel instead of classic php. A lot of tools are provided for you and will make your life alot easier. I recommend starting with codeigniter since its easier.

Exploit
  • 6,278
  • 19
  • 70
  • 103
0

While uploading the file you can easily change the name of the file.

$filename = $_FILES['file']['name'];
$file_ext = substr($filename, strripos($filename, '.'));

$new_filename = UserId.Username.$file_ext;

$tempFile = $_FILES['file']['tmp_name'];

$targetFile = yourpath.$new_filename;

move_uploaded_file($tempFile, $targetFile);
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Happy Coding
  • 2,517
  • 1
  • 13
  • 24