I'm trying to make a record where I have uploads
of images and need to save all this content in different tables. The text part is saving perfectly however the photo part is still not saving.
Here is the PHP code that saves the data to the database:
<?php
// incluindo o arquivo que faz a conexao com o banco
include ("../includes/conexao.php");
include ("../includes/suc_validacao.php");
include ("../includes/suc.php");
$cdClie = $_POST['COD_IDENT_CLIEN'];
$titulo = $_POST['TXT_TITUL_PUBLI'];
$status = $_POST['FLG_STATU_PUBLI'];
$resumo = $_POST['TXT_RESMO_PUBLI'];
$msg = $_POST['TXT_DETLH_PUBLI'];
$link = $_POST['TXT_LINKX_URLXX'];
$usurLoga = $_SESSION['UsuarioID'];
$imagem = $_FILES["imagem"];
$query = "INSERT INTO tbl_PUBLICACOES (COD_IDENT_CLIEN, TXT_TITUL_PUBLI, FLG_STATU_PUBLI, FLG_TIPOX_PAGIN, TXT_RESMO_PUBLI, TXT_DETLH_PUBLI, TXT_LINKX_URLXX, COD_IDULT_ATUAL, DAT_ULTIM_ATUAL) VALUES";
$query .= "('$cdClie','$titulo','$status','012','$resumo','$msg','$link','$usurLoga', now())";
$inserir = mysql_query($query)
or die(error());
$response = array("success" => true);
// Recebeu a imagem
if($imagem != NULL):
$nomeFinal = time().'.jpg';
// Tenta gravar o arquivo no servidor
if (move_uploaded_file($imagem['tmp_name'], $nomeFinal)):
// Pega a imagem
$tamanhoImg = filesize($nomeFinal);
$mysqlImg = addslashes(fread(fopen($nomeFinal, "r"), $tamanhoImg));
$linkIMG = $_POST['TXT_LINKX_URLXX'];
$msgImg = $_POST['MEM_LEGEN_IMAGM'];
// Conecta-se ao BD e tenta gravar
try{
$query = mysqli_query("INSERT INTO tbl_IMAGENS (BLO_CONTE_IMAGM, MEM_LEGEN_IMAGM, TXT_LINKX_URLXX, COD_IDULT_ATUAL, DAT_ULTIM_ATUAL ) VALUES ('$mysqlImg', '$msgImg', '$linkIMG', '$usurLoga', now())");
} catch( PDOException $e ) {
echo $e->getMessage();
}
// Apaga o arquivo
unlink($nomeFinal);
endif;
else:
echo"Você não realizou o upload de forma satisfatória.";
endif;
//fechando a conexao com o banco
mysql_close($conn);
header("Location: publicacaoPF.php?P_COD_IDENT_CLIEN=$cdClie"); exit; // Redireciona o visitante
?>
Page code where I send the image:
<input id="files" name="imagem" type="file" multiple/>
<output id="result" />
But my project has a peculiarity I do preview
of the image and along with preview
I indicate 2 more fields to be added for each image, one of these fields is the caption and the link if it exists, I do this using jquery
.
Follow the jquery code:
<script>
window.onload = function(){
//Check File API support
if(window.File && window.FileList && window.FileReader)
{
var filesInput = document.getElementById("files");
filesInput.addEventListener("change", function(event){
var files = event.target.files; //FileList object
var output = document.getElementById("result");
for(var i = 0; i< files.length; i++)
{
var file = files[i];
//Only pics
if(!file.type.match('image'))
continue;
var picReader = new FileReader();
picReader.addEventListener("load",function(event){
var picFile = event.target;
var div = document.createElement("div");
div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +
"title='" + picFile.name + "'/> <a href='#' class='remove_pict del'><i class=\"icon-remove\"></i></a> <div class=\"control-group\"> <label class=\"control-label\" for=\"MEM_LEGEN_IMAGM\">Legenda</label> <div class=\"controls\"><input id=\"MEM_LEGEN_IMAGM\" name=\"MEM_LEGEN_IMAGM\" type=\"text\" class=\"input-xlarge\"> </div> </div> <div class=\"control-group pull-right linkImg\"><label class=\"control-label\" for=\"TXT_LINKX_URLXX\">Link</label><div class=\"controls\"><input id=\"TXT_LINKX_URLXX\" name=\"TXT_LINKX_URLXX\" type=\"text\" class=\"input-xlarge\"> </div> </div>";
output.insertBefore(div,null);
div.children[1].addEventListener("click", function(event){
div.parentNode.removeChild(div);
});
});
//Read the image
picReader.readAsDataURL(file);
}
});
}
else
{
console.log("Your browser does not support File API");
}
}
The error you are giving is:
[Fri Jun 26 12:37:24 2015] [error] [client 192.168.1.105] PHP Warning: move_uploaded_file (1435311444.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /opt/lampp/htdocs/renan/jrassessoria/admin/paginas/C_SalvaPublicacaoPF.php on line 35, referer: link Warning: move_uploaded_file () [function.move-uploaded-file]: [PHP] Unable to move '/ tmp / phpMAzIYb' to '1435311444.jpg' in /opt/lampp/htdocs/renan/jrassessoria/admin/paginas/C_SalvaPublicacaoPF.php on line 35, referer: link
After changing some things in the code, this error occurred:
[function.filesize]: stat failed for 1435324431.jpg> in / opt / lampp
[Fri Jun 26 16:13:51 2015] [error] [client 192.168.1.105] PHP Warning: filesize () /htdocs/renan/jrassessoria/admin/paginas/CalendarPublicacaoPJ.php on> line 31, referer: > link ? > P_COD_IDENT_CLIEN = 17 [Warning: Fopen (1435324431.jpg) [function.fopen]: failed to open> stream: No such file or directory in > /opt/lampp/htdocs/renan/jrassessoria/admin/paginas/CalendarPublicationPJ.php on line> 32, referer:> link ?> P_COD_IDENT_CLIEN = 17 [Fri Jun 26 16:13:51 2015] [error] [client 192.168.1.105] PHP Warning: fread ()> expects parameter 1 to be boolean given in & opt / lampp / htdocs / renan / jrassessoria /admin/paginas/CalendarPublicationPJ.php on line > 32, referer: > link ? > P_COD_IDENT_CLIEN = 17 [Fri Jun 26 16:13:51 2015] [error] [client 192.168.1.105] PHP Notice: Undefined> index: MEM_LEGEN_IMAGM in > /opt/lampp/htdocs/renan/jrassessoria/admin/paginas/CalendarPublicationPJ.php on line>
>
It's even saving, but it does not take my caption and link fields.