Solved

PHP - move_uploaded_file doesn't work when my action is to open another php page

Posted on 2015-02-02
2
327 Views
Last Modified: 2015-02-03
I am using a php page to open a file and save it to a /tmp/ directory of a web server. The problem I am facing is when I use the same code where I do the operation of upload file on the same page it works perfect.
It doesn't work when I want to open a new php page by posting to open a new php page. Is this a limitation or how can I achieve it to work?

if($btnAction == "Create Initiators") {
   $localFile = "";
   $finfo     = finfo_open(FILEINFO_MIME_TYPE);
   $ftype     = finfo_file($finfo, $_FILES["file"]["tmp_name"]);
   finfo_close($finfo);

   if($_FILES["file"]["type"] == "text/csv" || $ftype == "text/csv" || $ftype == "text/plain") {
       if ($_FILES["file"]["error"] > 0) {
           echo "Error: " . $_FILES["file"]["error"] . "<br />\n";
       } else {
           $localFile = "/tmp/".$_FILES["file"]["name"];
           if (move_uploaded_file($_FILES["file"]["tmp_name"], $localFile)) {
               echo "File is uploaded.\n";
           } else {
               echo "Upload Failed.\n";
           }
       }

   }
   else {
       echo "Invalid File";
   }
}

The above code works perfectly if I have the below.
echo "<form name=\"form1\" method=\"post\" enctype=\"multipart/form-data\">";

It doesn't work if I have the below.
echo "<form name=\"form1\" method=\"post\"  action=\"scriptExecution.php\" enctype=\"multipart/form-data\">";

But my intention is to copy the file to /tmp/ directory of the webserver and then open a new page where I can do further processing.

Any help on how I can get this fixed?
0
Comment
Question by:naveenraj
2 Comments
 
LVL 9

Accepted Solution

by:
Rob_Jeffrey earned 500 total points
ID: 40583777
Whichever page is the target of the form's action should receive the posted data and the file handle information.  Is the code
if($btnAction == "Create Initiators") { ...

part of your source page, or is it in scriptExecution.php?  It should be within the scriptExecution.php
0
 

Author Closing Comment

by:naveenraj
ID: 40585796
thanks for the quick response , I have used the posted data in the form's action page and it worked.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question