hi, i have to pieces of php code that i need to place into one but am unsure how to do it, firstly i have my peice of php to upload a few statistics:

<?php
include 'mysqlconnect.php';

$db = mysql_select_db("upload_frenzy", $con);

if (!$db) 
{
    die('Can not Connect to Database: ' . mysql_error());
}

$file_name =$_POST['file_name'];
$file =$_POST['file'];
$file_description =$_POST['file_description'];
$user_ip =$_SERVER['REMOTE_ADDR'];

$sql="INSERT INTO uploads (file_name, file, file_description, user_ip)
VALUES
('$file_name','$file','$file_description','$user_ip')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
	header('location:home');
	
mysql_close($con)

?>

And secondly i have a piece of code straight from w3schools which i will later modify to suit my needs:

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>

Could someone please help me in putting these two pieces of code together so they work with each other?

This code will work...

<?php

$file =$_POST['file'];
$file_description =$_POST['file_description'];
$user_ip =$_SERVER['REMOTE_ADDR']; 
$file_name =$_FILES["file"]["name"];

// $file and $file_name are same.. So use anyone to store it in the database...

if ($_FILES["file"]["error"] > 0)
{  
echo "Error: " . $_FILES["file"]["error"] . "<br />";  
}
else  
{  
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";  
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";  
echo "Stored in: " . $_FILES["file"]["tmp_name"];  
}

include 'mysqlconnect.php'; 
$db = mysql_select_db("upload_frenzy", $con);
 if (!$db) 
 {    
 die('Can not Connect to Database: ' . mysql_error());
 } 
 
 $sql="INSERT INTO uploads (file_name, file, file_description, user_ip)VALUES('$file_name','$file','$file_description','$user_ip')"; 
 if (!mysql_query($sql,$con))  
 {  
 die('Error: ' . mysql_error());  
 }	
 header('location:home'); 
 mysql_close($con) 
?>

Hope you got it.. If still any probs, pls let me know....

thank you for your reply Dragon, the file_name isn't being stored in the database and im unsure why, and, similarly this code should also work correct?

<?php

$file =$_POST['file'];
$file_description =$_POST['file_description'];
$user_ip =$_SERVER['REMOTE_ADDR']; 
$file_name =$_FILES["file"]["name"];

// $file and $file_name are same.. So use anyone to store it in the database...

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("uploads/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }

include 'mysqlconnect.php'; 
$db = mysql_select_db("upload_frenzy", $con);
 if (!$db) 
 {    
 die('Can not Connect to Database: ' . mysql_error());
 } 
 
 $sql="INSERT INTO uploads (file_name, file, file_description, user_ip)VALUES('$file_name','$file','$file_description','$user_ip')"; 
 if (!mysql_query($sql,$con))  
 {  
 die('Error: ' . mysql_error());  
 }	
 header('location:home'); 
 mysql_close($con) 
?>

$file_name has name of your uploaded file name. This will be in string format. So, check your field datatype is in varchar format. Additionally, store the file name in detailed format (i.e) stored as uploads/filename.

So, use this line before insert into your database.

$file_name="uploads/".$_FILES["file"]["name"];
This should works., Check it...

ok, well i have at least something in the file_name field now, and this will do for a while, along with this, should the file that i am uploading get saved in uploads/'filename' with this piece of code? if so it isn't, and this is another problem that i cant solve... Any idea why? or doesnt it actually save to my pc?

Thanks Again...

Yeah., your uploaded file should saved in uploads/filename. Once you retrieve from database, it seeks that filepath and based on this only that file get recovered. There will be no probs., What difficulty you have faced..?

the difficulty is that it isn't actually saving any files to that destination folder, not sure why, any ideas?

Try this code for uploading the file...

$target_path="uploads/";

if(move_uploaded_file($_FILES['uploadval']['tmp_name'],$target_path.$_FILES['uploadval']['name'])) 
{
echo "File uploaded successfully..";
}
else
{
echo "Sorry.. File doesn't uploaded..";
}

This is working fine.. Try this and let me if any probs..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.