Solved

how to use a parameter as a value for $_files (php upload problems)

Posted on 2014-04-13
7
644 Views
Last Modified: 2014-04-14
I made this following example:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Documento sin título</title>
</head>

<body>
   <form name="configurar" id="configurar" enctype="multipart/form-data" method="POST" action="<?php echo $miFormAction; ?>">
<input accept="image/jpeg" type="file" id="archivo" name="archivo" size="46"  /><input type="submit" value="Enviar" name="B3"></td>

       <input type="hidden" name="max_file_size" value="52428800" />
       <input type="hidden" name="MM_insert" value="configurar">
</form>
</body>
</html>
<?php


$miFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $miFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}



 if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "configurar")) 
 {
	 file_upload('',$_FILES['archivo']['name'],'El archivo se pudo subir'); 
 }


function file_upload($ruta,$miarchivo,$mensaje)
{

   if(move_uploaded_file ($_FILES[$miarchivo]['tmp_name'],$ruta.$miarchivo))
   {   
    echo "<script type=\"text/javascript\">alert(\"".$mensaje."\");</script>";  
   }	
}
?>

Open in new window

I would like to know if it is possible to use a parameter as name of a file like this example
0
Comment
Question by:altariamx2003
  • 3
  • 2
  • 2
7 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39998352
I do not see where you are trying to do that.  ??
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39998356
Here are the PHP Docs on uploading files: http://www.php.net/manual/en/features.file-upload.php
0
 
LVL 56

Expert Comment

by:Julian Hansen
ID: 39998368
Do you mean here

move_uploaded_file ($_FILES[$miarchivo]['tmp_name'],$ruta.$miarchivo)

Open in new window


Seems you have answered your own question then.

In PHP you can use a variable almost anywhere you have a constant string i.e.

$_FILES['myfile']
vs
$fileid = 'myfile';
$_FILES[$fileid];

If not can you be more specific about what you are asking?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:altariamx2003
ID: 39999150
What Im trying to do is send the name of a file that I want to upload to a function as a parameter.

And inside the function use "move_uploaded_file" use the parameter to upload a file

I try to do  as you see in my example, but the file isnt uploaded

you can see the example in action in this link: http://www.grupossc.com/prueba.php
0
 
LVL 56

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39999280
Try this - change your call and file_upload function to
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "configurar")) 
{
  file_upload('','archivo','El archivo se pudo subir'); 
}


function file_upload($ruta,$miarchivo,$mensaje)
{
  if(move_uploaded_file ($_FILES[$miarchivo]['tmp_name'],$ruta.$_FILES[$miarchivo]['name']))
  {   
    echo "<script type=\"text/javascript\">alert(\"".$mensaje."\");</script>";  
   }	
}
?>

Open in new window

You were passing the name of the uploaded file to the function instead of the name of file input control.
0
 

Author Comment

by:altariamx2003
ID: 39999287
I works!!!, thanks a lot julian!!!!
0
 
LVL 56

Expert Comment

by:Julian Hansen
ID: 39999590
You are welcome - thanks for the points.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Find out what you should include to make the best professional email signature for your organization.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

685 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