Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-02-02
2
Medium Priority
?
353 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 2000 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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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.:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

885 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