Jump to content

Recommended Posts

Hi

 

I'm trying to upload a file into my server and save the url path into my database

<?php

header('Content-Type: application/json');
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');

$mysqli = new mysqli('localhost', 'xxxxx', 'xxxxxx');
if (mysqli_connect_errno()) {
    trigger_error('Database connection failed: ' . mysqli_connect_error(), E_USER_ERROR);
}

$dirpath = realpath(dirname(getcwd()));
$target_dir =  $dirpath ."/uploads/";  
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$Filename= addslashes(file_get_contents($_FILES['fileToUpload']['tmp_name']));
$file_path = $dirpath.basename($Filename);

$escola = $_POST['escola'];
$form = $_POST['form'];
$data = $_POST['data'];
$horas = $_POST['horas'];
$local = $_POST['local'];
$dest = $_POST['dest'];
$datas = $_POST['datas'];

$visto = 0;


if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)) {
$sql = "INSERT INTO `ebspma_paad_ebspma`.`formacoes`(idescola, nome, inicio, horas, local, destinatarios, dataLimite, visto, path) VALUES(?, ?, ?, ?, ? ,?, ?, ?, ? )";
$stmt = $mysqli->prepare($sql);
if ($stmt === false) {
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $mysqli->error, E_USER_ERROR);
}
$stmt->bind_param('issssssib', $escola, $form, $data, $horas,$local,$dest, $datas, $visto, $file_path );
if (!$stmt->execute()) {
    echo json_encode(array('status' => 'error', 'message' => 'Opppss...A formação não foi gravada'));
}
}
else { 
   echo json_encode(array('status' => 'error', 'message' => 'Opppss...A formação não foi gravada'));
}
$stmt->close();
echo json_encode(array('status' => 'success', 'message' => 'Nova formação gravada')); 

The file is saved into my server but the path is not saved into my database....

 

whats wrong?

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/296483-saving-path-in-database/
Share on other sites

This code:

$stmt->bind_param('issssssib', $escola, $form, $data, $horas,$local,$dest, $datas, $visto, $file_path );
if (!$stmt->execute())
{
echo json_encode(array('status' => 'error', 'message' => 'Opppss...A formação não foi gravada'));
}
}
else
{
echo json_encode(array('status' => 'error', 'message' => 'Opppss...A formação não foi gravada'));
}
$stmt->close();

 

is flawed and is probably causing your script to not execute. You think that the file is being saved but I suggest that it was already there.

 

Try adding some echo statements to verify what is happening - after you fix the flaw above.

To point out further: or, question?

$stmt->bind_param('issssssib', $escola, $form, $data, $horas,$local,$dest, $datas, $visto, $file_path );

Why are you sending the file path as a blob? 

If you need it to be a blob, you couldn't send it with mysqli-stmt.execute. You would need to send it with mysqli-stmt.send-long-data, this fact is noted in the manual on mysqli-stmt.bind-param.

 

Hi

 

Thank you, this was the problem, but now i'm facing another one with this code

I have this

$target_dir =  $dirpath ."/uploads/";

But what i get saved into my database is

 

/home/ebspma/public_htmlhtml>

 

and it should be

 

http://ebsp.comt/uploads/filename

 

What is wrong with my code?

But what i get saved into my database is

 

/home/ebspma/public_htmlhtml>

 

and it should be

 

http://ebsp.comt/uploads/filename

 

What is wrong with my code?

 

You currently use getcwd() to define $dirpath:

$dirpath = realpath(dirname(getcwd()));

That function returns the working path. More information can be found here:

http://php.net/manual/en/function.getcwd.php

 

You'll need to find an alternative way to add the domain name. You could start here:

https://www.google.com/search?q=php%20get%20domain%20name&rct=j

 

Or...have you tried using a root-relative link? Instead of "http://ebsp.comt/uploads/filename", you could try "/uploads/filename".

Personally I wouldn't store the whole path and would just store the filename and create the path when outputting (or define it as a variable and use that when outputting). What if you decide to change where you store the images down the road? They're all hardcoded in your db.

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.