Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How can I keep the orginal name of the uploaded image?

2»

Comments

  • I just tried to change the theme from Mobile version 1.1.3 to Embed-Friendly version 3 but this didn't affect the result. The modified version still results in a whoops message, the unmodified gives the normal wysiwyg editor window.

  • I have found a solution to my problem! I had hoped that there would be a simple way of modifying the code (cf. my very first post above).

    I thought that the random number name for the folder(s) could simply be replaced by the UserID. As I don't have any php programming skills at all I didn't know how to grasp the UserID to have inserted in the folder path. I found this function in the code suggested by @hgtonight:

    $UserID = Gdn::Session()->UserID;

    I inserted it and replaced: $Subdir = sprintf('%03d', mt_rand(0, 999)).'/';

    with: $Subdir = $UserID . '/';

    After that the code looks like this:

    public function PostController_Imageupload_create()
    {
    try {
    $UploadImage = new Gdn_UploadImage();
    $TmpImage = $UploadImage->ValidateUpload('image_file');
    $UserID = Gdn::Session()->UserID;

            // Generate the target image name. 
            // $TargetImage = $UploadImage->GenerateTargetName(PATH_UPLOADS.'/imageupload', '', TRUE);
                                $TargetFolder = PATH_UPLOADS.'/forumbilleder';
                                $Name =$UploadImage->GetUploadedFileName();
                                $Subdir = $UserID . '/';
                                $TargetImage = "$TargetFolder/{$Subdir}$Name";
    
            $Props = $UploadImage->SaveImageAs($TmpImage,$TargetImage,C('Plugins.UploadImage.MaxHeight',''),C('Plugins.UploadImage.MaxWidth',650));
            echo json_encode(array('url'=>$Props['Url'],'name'=>$UploadImage->GetUploadedFileName()));
        } catch (Exception $e) {
            header('HTTP/1.0 400', TRUE, 400);
            echo $e;
        }
    

    This is pretty much what @hgtonight suggested from start but something in the remainder of the code apparently was incompatible with my program installation. Anyway, changing these two lines was sufficient. Now all images from a user are placed in a separate folder with the name (= UserID number) of that member. And that was exactly what I was looking for!

    Thank you very much for your valuable advice!

Sign In or Register to comment.