Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8545421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:47:14+00:00 2026-06-11T12:47:14+00:00

If a user uploads a file and the contents are retrieved like so $file

  • 0

If a user uploads a file and the contents are retrieved like so

$file = $_FILES['uploadedFile'];

Then, the file is sent to a function to make sure it’s an accepted file type. If it is, save it on the server

function saveInputFile($file){
   if($check->checkFile($file)== TRUE){
      //save $file on my server
   }
   else{
      echo "can't be saved!";
   }
}

Assuming it passes the checkFile function, how can I then save this file to my server from within the saveInputFile function? Can I set the file equal to a variable, and then save that variable or do I have to save the file directly from the POST data?

I’ve seen it done like this, but I already have the file passed into this function.

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
   echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";
 } else{
    echo "There was an error uploading the file, please try again!";
}

When it comes down to it, I want to save a file in the following function. Can I pass the file like a variable as I did in the saveInputFile function above, or does it not work like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:47:15+00:00Added an answer on June 11, 2026 at 12:47 pm

    You can make the upload file a type of it’s own that has the methods it needs next to it’s data. That will make the usage more flexible:

    $upload = new UploadFile($_FILES['uploadedFile']);
    $message = saveInputFile($upload);
    echo $message;
    
    function saveInputFile(UploadFile $upload)
    {
        if ($check->checkFile($upload->getBasename()) == TRUE) {
            $message = $upload->moveTo($target_path)
                ? sprintf('The file %s has been uploaded', $upload->getBasename())
                : 'There was an error uploading the file, please try again!'
                ;
        } else {
            $message = "can't be saved!";
        }
    
        return $message;
    }
    

    The new type UploadFile represents one file from the the $_FILE array, it is a class that wraps the basic data and methods a file-upload carries. Here is some example code:

    class UploadFile
    {
        protected $file;
    
        private $filename;
    
        public function __construct(array $file)
        {
            $this->file = $file;
        }
    
        public function hasError()
        {
            return $this->getProperty('error') !== UPLOAD_ERR_OK;
        }
    
        public function getError()
        {
            return $this->getProperty('error');
        }
    
        public function getBasename()
        {
            return basename($this->getProperty('name'));
        }
    
        public function getFilename()
        {
            return $this->filename;
        }
    
        /**
         * @param $newName
         * @return NULL|SplFileInfo
         * @throws BadMethodCallException
         */
        public function moveTo($newName)
        {
            $newName  = (string)$newName;
            $filename = $this->getFilename();
    
            if ($filename !== NULL) {
                throw new BadMethodCallException(sprintf('Upload file (%s) has been already moved (%s).', $this->getBasename(), $filename));
            }
    
            $tmpName = $this->getProperty('tmp_name');
    
            if (move_uploaded_file($tmpName, $newName)) {
                $this->filename = realpath($newName);
            }
    
            return $this->getFileInfo();
        }
    
        /**
         * @return SplFileInfo|NULL
         */
        public function getFileInfo()
        {
            $filename = $this->getFilename();
    
            if ($filename !== NULL) {
                return new SplFileInfo($filename);
            }
        }
    
        protected function getProperty($name, $default = NULL)
        {
            if (isset($this->file[$name])) {
                return $this->file[$name];
            }
    
            return $default;
        }
    }
    

    Use it at your will. See as well SplFileInfo and the documentation about file uploads in the PHP manual which documents the structure of the $_FILE array and the PHP upload error codes (which are important).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I get the contents of a local file, say: /home/user/Wired/uploads/1.csv in a
If the user uploads some file to my server and I want to make
I made an upload system where the user uploads a file and that file
I have a very simple page with which a user uploads a file. Using
An app uses paperclip to handle file uploads for user avatars. Paperclip currently deletes
I created uploads folder on the server, and a file uploaded by a user
I have a small form in which the user uploads a file. What I
I want to have a user select a file and then have php put
I created a content type and there is a file upload field. The user
I have a system where the user uploads a file and I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.