Solved

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

Posted on 2015-02-02
2
319 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

896 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now