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
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";
}
?>

 

 

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.