Jump to content

[SOLVED] Uploading a file to a server


jej1216

Recommended Posts

I found this old post : http://www.phpfreaks.com/forums/index.php/topic,93267.0.html

which has most of the answers to what I want to do.  I need to have a user upload files to a location on the server.  Here is my code (thanks to the link above!):

 

<?php

  $location = "/forms";

  if (!$_FILES['file']['tmp_name']) {

      $form = "select a file to upload<br>";

      $form.="<form action='$PHP_SELF' method='post' enctype='multipart/form-data'>";

      $form.="  <input type='file' name='file' size='50'><br>";

      $form.="  <input type='submit' value='upload file!'>";

      $form.="</form>";

  } elseif($_FILES['file']['tmp_name'] != "none") {

      copy ($_FILES['file']['tmp_name'], $location.$_FILES['file']['name']) or die("could not upload file.");

      $form ="File upload succeeded...<br>";

      $form.="<ul><li>sent: ".$_FILES['file']['name'];

      $form.="<li>size: ".$_FILES['file']['size'];

      $form.="<li>type: ".$_FILES['file']['type'];

  } else {

      $form = "upload failed";

  }

  echo $form;

?>

 

When the page loads, I get these messages:

 

Notice: Undefined index: file in /usr/local/php_classes/forms/ehi_forms_upload.php on line 11

 

Notice: Undefined variable: PHP_SELF in /usr/local/php_classes/forms/ehi_forms_upload.php on line 13

select a file to upload

 

When I try to upload a file, I get this error:

 

Warning: copy(/formsphonelist.log): failed to open stream: Permission denied in /usr/local/php_classes/forms/ehi_forms_upload.php on line 18

could not upload file.

 

I know this is close to what I need --- but what does this error mean? 

 

TIA,

 

jej1216

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/77235-solved-uploading-a-file-to-a-server/
Share on other sites

Thanks - we use Joomla, and register_globals needs to be turned off for Joomla to work.  /$PHP_SELF can not be used because it will not work without register_globals being enabled.  I changed the code to this:

 

 

<?php

  $location = "/usr/local/php_classes/forms/";

  if (!$_FILES['file']['tmp_name']) {

      $form = "select a file to upload<br>";

      $form.="<form  method='post' enctype='multipart/form-data'>";

      $form.="  <input type='file' name='file' size='50'><br>";

      $form.="  <input type='submit' value='upload file!'>";

      $form.="</form>";

  } elseif($_FILES['file']['tmp_name'] != "none") {

      copy ($_FILES['file']['tmp_name'], $location.$_FILES['file']['name']) or die("could not upload file.");

      $form ="File upload succeeded...<br>";

      $form.="<ul><li>sent: ".$_FILES['file']['name'];

      $form.="<li>size: ".$_FILES['file']['size'];

      $form.="<li>type: ".$_FILES['file']['type'];

  } else {

      $form = "upload failed";

  }

  echo $form;

?>

 

 

I also had to chmod 777 the folder I was uploading to.  I still have the "Notice: Undefined index: file in /usr/local/php_classes/forms/ehi_forms_upload.php on line 11" issue, but I think that is an undefined variable in the line 

 

if (!$_FILES['file']['tmp_name']) {

 

 

Thanks,

 

jej1216

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.