Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP move_uploaded_file() Function
Next article icon

PHP move_uploaded_file() Function

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The move_uploaded_file() function is an inbuilt function in PHP that is used to change the file destination. This function only works when the file is uploaded by the PHP POST function. If the file is valid it will uploaded.

Syntax:

move_uploaded_file( string $from, string $to ): bool

Parameters: This function accepts two parameters that are described below.

  • $from: This parameter specifies the temporary location of the file. This is the location where your uploaded file is stored temporarily.
  • $to: This parameter specifies the destination of the location where your file will be stored.

Return Value: This function returns true if the function successfully changes the location of the file otherwise this function will return false.

Program 1: The following program demonstrates the move_uploaded_file() function.

PHP
/* upload.php */ <?php  if ($_SERVER["REQUEST_METHOD"] == "POST") {     if (         isset($_FILES["file_upload"]) &&         $_FILES["file_upload"]["error"] == UPLOAD_ERR_OK     ) {           // Add name of upload directory         $uploadDirectory = "./uploads/";          if (move_uploaded_file(             $_FILES["file_upload"]["tmp_name"],             $uploadDirectory . $_FILES["file_upload"]["name"]             )) {             echo "File uploaded successfully!";         } else {             echo "Error moving file.";         }     } else {         echo "Error uploading file.";     } }  ?> 
HTML
<!-- index.php --> <!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport" content=         "width=device-width, initial-scale=1.0">     <title>Simple File Upload</title> </head>  <body>     <form action="upload.php" method="post"         enctype="multipart/form-data">                  <label for="file_upload">             Choose a file:         </label>                  <input type="file" name="file_upload"              id="file_upload">                  <input type="submit" value="Upload File">     </form> </body>  </html> 

Output:

uploaded

Program 2: The following program demonstrates the move_uploaded_file() function. In this example we are uploading only specific extensions.

PHP
/* upload.php */ <?php  if ($_SERVER["REQUEST_METHOD"] == "POST") {     if (         isset($_FILES["file_upload"]) &&         $_FILES["file_upload"]["error"] == UPLOAD_ERR_OK     ) {         $uploadDirectory = "uploads/";          $originalFileName =              basename($_FILES["file_upload"]["name"]);         $fileExtension = pathinfo(             $originalFileName, PATHINFO_EXTENSION);          $allowedExtensions = ["jpg", "jpeg", "png"];          if (in_array($fileExtension, $allowedExtensions)) {             if (                 move_uploaded_file(                     $_FILES["file_upload"]["tmp_name"],                     $uploadDirectory . $originalFileName                 )             ) {                 echo "File uploaded successfully!";             } else {                 echo "Error moving file.";             }         } else {             echo "Error: Only JPG, JPEG, and "                 . "PNG files are allowed.";         }     } else {         echo "Error uploading file.";     } } ?> 
HTML
<!-- index.php --> <!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport" content=         "width=device-width, initial-scale=1.0">     <title>File Upload Example</title> </head>  <body>     <form action="upload.php" method="post"          enctype="multipart/form-data">          <label for="file_upload">             Choose a file (only JPG, JPEG, and PNG):         </label>                  <input type="file" name="file_upload"              id="file_upload" accept=".jpg, .jpeg, .png">                  <input type="submit" value="Upload File">     </form> </body>  </html> 

Output:

uploadedReference: https://www.php.net/manual/en/function.move-uploaded-file.php


Next Article
PHP move_uploaded_file() Function

N

neeraj3304
Improve
Article Tags :
  • PHP
  • PHP-function
  • PHP-Filesystem

Similar Reads

    PHP | is_uploaded_file( ) Function
    The is_uploaded_file() function in PHP is an inbuilt function which is used to check whether the specified file uploaded via HTTP POST or not. The name of the file is sent as a parameter to the is_uploaded_file() function and it returns True if the file is uploaded via HTTP POST. This function can b
    2 min read
    PHP is_file( ) Function
    The is_file() function in PHP is an inbuilt function which is used to check whether the specified file is a regular file or not. The name of the file is sent as a parameter to the is_file() function and it returns True if the file is a regular file else it returns False. Syntax: bool is_file($file)
    2 min read
    PHP | filesize( ) Function
    The filesize() function in PHP is an inbuilt function which is used to return the size of a specified file. The filesize() function accepts the filename as a parameter and returns the size of a file in bytes on success and False on failure. The result of the filesize() function is cached and a funct
    2 min read
    PHP | filetype( ) Function
    The filetype() function in PHP is an inbuilt function which is used to return the file type of a specified file or a directory. The filetype() function accepts the filename as a parameter and returns one of the seven file types on success and False on failure. The seven possible return values of the
    2 min read
    PHP | fileatime( ) Function
    The fileatime() function in PHP is an inbuilt function which is used to return the last access time of a specified file. The fileatime() function returns the last access time of a file as a Unix Timestamp on success and False on failure. The filename is passed as a parameter to the fileatime() funct
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences