Jump to content

Uploading User Files


FlashNinja

Recommended Posts

I'm trying out a script that lets users upload files into a directory, the file path then should be saved on the user information in the database. This script keeps throwing "Undefined index: file" errors, even though I;m sure it should be defined. Could someone take a look please?

 

Here's the form I'm using:

 


<form id ='change0' action ='pic_up.php' method ='post'
    accept-charset='UTF-8'>
<fieldset >
<legend>Confirm Details</legend>
<input type ='hidden' name ='file' id ='file' value ='800000'/>
<label for ='file' >Upload Profile Picture:</label>
<input type ='file' name ='file' id ='file' />
<input type ='submit' name ='Submit' value ='Submit' />
</fieldset>
</form>

 

Here's the PHP script:

 

<?php
  include 'connect.php';
  
  session_start();
  $_SESSION['username']; 
  $username = $_SESSION['username'];
  
  if(!(isset($_SESSION['login']) && $_SESSION['login']!= " ")){
       header("Location: login.php");
   }
  
  $tablename = 'usr_test';
  
  $targ = "localhost/img/";
  $targ = $targ . basename($_FILES['file']['name']);
    
  $file = ($_FILES['file']['name']);
  
  
  mysql_query("INSERT INTO $tablename (pic) VALUES ($file) WHERE usr = '$username'");
  
  if(move_uploaded_file($_FILES['file']['tmp_name'], $targ))
    {
echo "File ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, not happening";
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/260821-uploading-user-files/
Share on other sites

Thanks, that fixed it so it actually runs, but now there's an error with line 22.

 

"Warning: move_uploaded_file(c: mpp\htdocs\img\community-nbc-dan-harmon-abed-evolution-320.jpg) [function.move-uploaded-file]: failed to open stream: Invalid argument in C:\xampp\htdocs\pic_up.php on line 22

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php45A3.tmp' to 'c: mpp\htdocs\img\community-nbc-dan-harmon-abed-evolution-320.jpg' in C:\xampp\htdocs\pic_up.php on line 22"

 

I'm not quite sure what on line 22 is actually causing this error.

 

PHP script:

 

<?php
  include 'connect.php';
  
  session_start();
  $_SESSION['username']; 
  $username = $_SESSION['username'];
  
  if(!(isset($_SESSION['login']) && $_SESSION['login']!= " ")){
       header("Location: login.php");
   }
  
  $tablename = 'usr_test';
  
  $targ = "c:\xampp\htdocs\img\\";
  $targ = $targ . basename($_FILES['file']['name']);
    
  $file = ($_FILES['file']['name']);
  
  
  mysql_query("INSERT INTO $tablename (pic) VALUES ('$file') WHERE usr = '$username'");
  
  if(move_uploaded_file($_FILES['file']['tmp_name'], $targ))
    {
echo "File ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, not happening";
}
?>

 

 

c: mpp\htdocs\img\community-nbc-dan-harmon-abed-evolution-320.jpg seems wrong doesn't it?

 

Apache will support using forward slashes in your local paths: c:/webserver/www/

 

Alternately, you can just use $_SERVER['DOCUMENT_ROOT']. This should return C:/xampp/htdocs/

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.