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

x
?
Solved

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

Posted on 2014-04-13
7
Medium Priority
?
648 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 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 60

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 60

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 60

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

927 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