Question

I am having an issue while uploading a file, my code is here:

if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['DOB']) && isset($_POST['address']) && isset($_POST['phone']) && isset($_FILES['file'])) {
    # code...
$n=mysql_real_escape_string(htmlspecialchars($_POST['name']));
$e=mysql_real_escape_string(htmlspecialchars($_POST['email']));
$dob=mysql_real_escape_string(htmlspecialchars($_POST['DOB']));
$a=mysql_real_escape_string(htmlspecialchars($_POST['address']));
$p=mysql_real_escape_string(htmlspecialchars($_POST['phone']));
$t=time();
$d_of_reg=date('D M Y @ H.i.s', $t);

$cv=$_FILES['file']['name'];



// connect to the db here

mysql_connect('localhost','root','') or die('Please we could not connect.. Try-again!!!');
mysql_select_db('joblessmen')or die('Please we could not connect to our database... Try-again!!!');

$sql="INSERT INTO `users`(`name`, `email`, `DOB`, `address`, `phone number`, `id`, `date_of_reg`) VALUES ('$n','$e','$dob','$a','$p','','$d_of_reg')";

if($sql_run=mysql_query($sql))
{
    echo "Thank you for registering. Joblessmen";
}



}

So I want to get the files to the server and it's telling me nothing when I echo $_FILE['file']['name'] Please what I have to do? My wamp settings in php_fileinfo are on, any help would be appreciated.

please this is the other part for uploading the file which is not working i want it to upload a jpg and doc,docx,pdf file only this is my php code

if ( isset($_FILES['file']['name']) && isset($_POST['name']) && isset($_POST['email']) && isset($_POST['DOB']) && isset($_POST['address']) && isset($_POST['phone'])) {
        # code...
    $n=mysql_real_escape_string(htmlspecialchars($_POST['name']));
    $e=mysql_real_escape_string(htmlspecialchars($_POST['email']));
    $dob=mysql_real_escape_string(htmlspecialchars($_POST['DOB']));
    $a=mysql_real_escape_string(htmlspecialchars($_POST['address']));
    $p=mysql_real_escape_string(htmlspecialchars($_POST['phone']));
    $t=time();
    $d_of_reg=date('D M Y @ H.i.s', $t);


    //$cv=$_FILES['file']['name'];

/*
    function upload_cv_check()
    {
        // Define some variables
        $max_size = 200000;
        $files_directory = $_SERVER['DOCUMENT_ROOT'].'/seabird/top-menu/cv';
        $file_name = basename($_FILES['file']['name']);
        $file_size = filesize($_FILES['file']['tmp_name']);
        $file_extension = strrchr($_FILES['file']['name'], '.'); 

        // Available extensions
        $extensions = array('.doc', '.pdf','.docx');

        // Return false if extension is not allowed
        if(!in_array($file_extension, $extensions))
            return false;

        // Return false if file size is over limit
        if($file_size > $max_size)
            return false;
            echo "file must be 2mb";

        // Fast way to rewrite file name, do your own
        $final_file_name = strtr($file_name, 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
        $final_file_name = preg_replace('/([^.a-z0-9]+)/i', '-', $final_file_name);

        // Move the file to your server
        move_uploaded_file($_FILES['my_image']['tmp_name'], $files_directory.$final_file_name);

        // Here is the link to your file, adapt it
        $file = 'http://'.$_SERVER['HTTP_HOST'].'/my_files_directory/'.$final_file_name;
    }
*/

    /*
    $extension=strtolower(substr($cv, strpos($cv, '.') + 1));
    $type=$_FILES['file']['type'];
    $size=$_FILES['file']['size'];
    $tmp_name=$_FILES['file']['tmp_name'];
    $max_size = 300000;

    if (isset($cv)) {

        if (!empty($cv)) {
            if (($extension == 'jpg' || $extension == 'jpeg' ) && $type=='image/jpeg' && $type=='image/jpg'  && $size<=$max_size) {
                $location='cv/';

                if (move_uploaded_file($tmp_name, $location)) {
                    echo "file has been uploaded";
                }
            }else {echo "there was an error";}
        }else{echo "file must be a image format.";}
        # code...
    }else{echo "please choose a file now";}
    */


    move_uploaded_file($tmp_name, $location);
    $tmp_name=$_FILES['file']['tmp_name'];
    $location='cv/';


    // connect to the db here

    mysql_connect('localhost','root','') or die('Please we could not connect.. Try-again!!!');
    mysql_select_db('joblessmen')or die('Please we could not connect to our database... Try-again!!!');

    $sql="INSERT INTO `users`(`name`, `email`, `DOB`, `address`, `phone number`, `id`, `date_of_reg`) VALUES ('$n','$e','$dob','$a','$p','','$d_of_reg')";

    if($sql_run=mysql_query($sql))
    {
        echo "Thank you for registering. Joblessmen";
        //echo $cv=$_FILES['file']['name'];
    }



    }

so i want it to upload a file from either of the extensions but it not working it is tell undefined index please what can i do

Was it helpful?

Solution

1) Make sure you have mentioned enctype="multipart/form-data" parameter

2) You can access the file by

$file=$_FILES['file']['name'];

$_FILES['name']['tmp_name'] will give you the temporary path of the file.

Then you can use the below function to move the file from temporary path to your file system.

move_uploaded_file($_FILES['name']['tmp_name'],"Your filesystem path");

OTHER TIPS


move_uploaded_file($_FILES['file']['tmp_name'] ,"upload/".$_FILES['file']['name']); $file= $_FILES['file']['name'];

Here is a code with multiple tests to do what you want :

// Define some variables
$max_size = 200000;
$files_directory = $_SERVER['DOCUMENT_ROOT'].'/my_files_directory/';
$file_name = basename($_FILES['my_image']['name']);
$file_size = filesize($_FILES['my_image']['tmp_name']);
$file_extension = strrchr($_FILES['my_image']['name'], '.'); 

// Available extensions
$extensions = array('.png', '.gif', '.jpg');

// Return false if extension is not allowed
if(!in_array($file_extension, $extensions))
    return false;

// Return false if file size is over limit
if($file_size > $max_size)
    return false;

// Fast way to rewrite file name, do your own
$final_file_name = strtr($file_name, 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
$final_file_name = preg_replace('/([^.a-z0-9]+)/i', '-', $final_file_name);

// Move the file to your server
move_uploaded_file($_FILES['my_image']['tmp_name'], $files_directory.$final_file_name);

// Here is the link to your file, adapt it
$file = 'http://'.$_SERVER['HTTP_HOST'].'/my_files_directory/'.$final_file_name;

Moreover, you need to have this line when opening your form :

<form method="your_method" action="your_action" enctype="multipart/form-data">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top