Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

747 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

9 Experts available now in Live!

Get 1:1 Help Now