Solved

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

Posted on 2015-02-02
2
336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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