Solved

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

Posted on 2014-04-13
7
643 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 55

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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 

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 55

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 55

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

809 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