Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-13
7
Medium Priority
?
647 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
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 84

Expert Comment

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

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 59

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 59

Accepted Solution

by:
Julian Hansen earned 2000 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 59

Expert Comment

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

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

722 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