Solved

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

Posted on 2014-04-13
7
639 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 82

Expert Comment

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

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 51

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 51

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 51

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 27
PHP connection to remote AWS MySQL RDS 4 36
addressing a specific html page 9 21
Wordpress Body Class 5 9
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now