0

PHP question on how to return a specific value from another class in a seperate file. So I have created a uploader class to handle and store files uploaded by users. But trying to connect the dots back to the orginal request which if for lets say a specific page - how can i pass a specific value back after I set the values in the other file... code example below.

Thanks Citti

//file 1 //store page request
public function store()
{
    //pass the uploaded file to be uploaded and stored
    $this->uploader->upload(RequestFile::file('assetfile'));

    $page = new Page;
        $page->user_id = $asset->user()->id;
        $page->assetfile = $file->user_id; <--- ? see below
    $page->save();
}

//file 2 //store physical file and create record in DB
class Uploader {

   public function upload($file)
   {
       $extension = $file->getClientOriginalExtension();
       $string = str_random(30);
       $uploadName = $string . '.' . $extension;
       $destinationPath = storage_path() . '/app/uploads/' . Request::user()->id;

       $file->move($destinationPath, $uploadName);

       $file = new File;
           $file->user_id = $asset->user()->id;
           $file->assetfile = $uploadId;
       $file->save();

       $return $file->user_id; ?!? 
0

2 Answers 2

1

What you could try is session variables?

Be sure to start sessions on both file 1 and file 2 via:

session_start();

Now, in your upload function, you can declare a session variable $_SESSION['specificValue'] = $valueSetInUpload;

Now, back on your first file you can access the variable inside of your store function with $valueSetInUpload = $_SESSION['specificValue'];

I believe this is what you are asking, but I could be wrong.

Sign up to request clarification or add additional context in comments.

2 Comments

I try to stay away from sessions as they can become corrupt and depending on behavior they can get hairy. also if i have a form that has multiple file uploads you wouldn't want this behavior. as you would want to return multiple fileIds for the seperate file uploads. thanks tho.
@Citti Interesting, I've never heard of corrupting sessions - also, sessions allow arrays.
0

To return values from a function, you use "return value" and you just have to store that to a variable or output it. Something like:

public function store() {
    $fileId = $this->uploader->upload(RequestFile::file('assetfile'));
    $page = new Page;
    $page->user_id = $asset->user()->id;
    $page->assetfile = $fileId;
    $page->save();
}

//file 2 //store physical file and create record in DB
class Uploader {
   public function upload($file) {
       $extension = $file->getClientOriginalExtension();
       $string = str_random(30);
       $uploadName = $string . '.' . $extension;
       $destinationPath = storage_path() . '/app/uploads/' . Request::user()->id;
       $file->move($destinationPath, $uploadName);
       $file = new File;
       $file->user_id = $asset->user()->id;
       $file->assetfile = $uploadId;
       $file->save();
       return $file->user_id;
   }
}

1 Comment

Yes. Thank you. Was driving me nuts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.