Everything executes but the query...what am I missing? - php

This is a file that receives uploaded images from another page. The images are received and saved on the server (that part is working) the query is not. I manually typed the query into phpMyAdmin with dummy values for the file name and it works. I put that dummy query into this code and it doesn't work. I can't figure out what I'm doing wrong.
<?php
$ds = DIRECTORY_SEPARATOR;
$storeFolder = 'img';
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT']. $ds. $storeFolder . $ds;
$targetFile = $targetPath. $_FILES['file']['name'];
move_uploaded_file($tempFile,$targetFile);
mysql_query("INSERT INTO `photos` (photo_id, file_name) VALUES ('', '".$_FILES['file']['name']."');");
}
?>

Assuming that photo_id is auto incremented here are some fixes to your code:
<?php
$ds = DIRECTORY_SEPARATOR;
$storeFolder = 'img';
if (!empty($_FILES)) {
$name = $_FILES['file']['name'];
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT']. $ds. $storeFolder . $ds;
$targetFile = $targetPath. $_FILES['file']['name'];
move_uploaded_file($tempFile,$targetFile);
$query = "INSERT INTO photos (photo_id, file_name) VALUES (NULL, '".$name."')";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
}
?>
This way you should be able to debug your code.
Now if you need to get the result from a Javascript call, you should use AJAX.
Check here:
Send a request to a php page and then get back results using ajax

Related

PHP upload file change name [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I got an upload script and must to change file name first of uploading.
Here is my code:
<?php
$ds = DIRECTORY_SEPARATOR;
$fileName = $_POST['articleSlug'];
$storeFolder = '../uploads';
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
$targetFile = $targetPath. $_FILES['file']['name'];
move_uploaded_file($tempFile,$targetFile);
}
?>
I must to use the $fileName variable to change filename, but when I change the $targetFile variable it doesn't upload the file anymore, so I've reverted my code to the moment was working, can you please help me to find how to replace the original file name using the variable content?
Thanks a lot
The most likely error is that changing $targetFile with $fileName, you're trying to write the file to a place where you do not have sufficient permissions.
You probably need to do something like this:
<?php
$ds = DIRECTORY_SEPARATOR;
$fileName = $_POST['articleSlug'];
$storeFolder = '../uploads';
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
$targetFile = $targetPath . $fileName;
move_uploaded_file($tempFile,$targetFile);
}
?>
Also "move_uploaded_file" throws warnings when somethings goes wrong. You can visualize this warnings with the following lines at begining of the script:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Finally you can do a vardump of the two possible destination and compare them to detect whats going on.
var_dump($targetPath . $fileName, $targetPath. $_FILES['file']['name']);
Replace
$targetFile = $targetPath. $_FILES['file']['name'];
to
$ext = end(explode(".", $_FILES['file']['name']));
$targetFile = $targetPath. fileName . '.' . $ext;

FPDF error: Unsupported image type: image/jpeg

if(isset($_POST['sumit']))
{
$con = mysqli_connect("localhost","root","","school");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sname=$_POST['sname'];
$category=$_POST['category'];
$sadd=$_POST['sadd'];
$file_name=$_FILES['file']['name'];
$temp_name=$_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$target_path = "Newfolder1";
$sql="SELECT * FROM imagetable WHERE sname='$sname' and sadd='$sadd'and category='$category' and file='$target_path'";
$result1 = mysqli_query($con, $sql);
if(!mysqli_query($con, $sql)){
die( "Could not execute sql: $sql");
}
$row = mysqli_fetch_row($result1);
$file=$row[0];
$pdf=new fpdf();
$pdf->ADDPage();
$pdf->setfont('Arial','B', 16);
$pdf->Image($file,150,25,50,20,$file_type);
$pdf->Cell(0,10,'id',0,1);
$pdf->Cell(0,10,'sadd',0,1);
$pdf->Cell(0,10,'category',0,1);
$pdf->Cell(0,10,'sname',0,1);
$pdf->Output();}
This is giving me the image type but all this is being done through database
my image type is being defined in the db but I don't know. I'm gettting the error.
If anyone could find out the error please reply
The problem is, you're specifying the mime type in $file_type variable(like image/png or image/jpeg) and using that in the Image() method. You should only specify the image format. Possible values are (case insensitive): JPG, JPEG, PNG and GIF.
So first get the image format, like this:
$image_format = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
And then call the Image() method like this:
$pdf->Image($file,150,25,50,20,$image_format);
Edited:
Change your INSERT query like this,
$image_path = $target_path . DIRECTORY_SEPARATOR . $file_name;
if(move_uploaded_file($temp_name, $image_path)) {
$qry="INSERT INTO imagetable (file, sname, category, sadd, type,size, content ) values ('".$image_path."','".$sname."','".$category."','".$sadd."', '".$file_type."','".$file_size."', '".$Content."')";
mysqli_query($con,$qry) or die("error in $qry == ----> ".mysqli_error());
}
And change your SELECT statement like this,
// your code
$target_path = "Newfolder1" . DIRECTORY_SEPARATOR . $file_name;
$sql="SELECT * FROM imagetable WHERE sname='$sname' and sadd='$sadd'and category='$category' and file='$target_path'";
// your code
$pdf->Image("image/$user-signature.png" ,100, 213,-300);
This format works for me. Try removing $file and putting quotes around $filetype

image file is moved to its folder but image name not inserted into database

I am creating a website, in users pages, I just wanted to add avatar (image), hence used Dropzone >> js and css.
In my case, the PHP script moved the uploaded image into a folder successfully (without any error), but the image name is not inserted (updated) into my db table.
The user table has an avatar field for storage of the image name.
If anyone can help, is really there error in below code or ....
uploads.php
include ('../fonfig/connection.php');
$ds = DIRECTORY_SEPARATOR;
$id = $_GET['id'];
$storeFolder = 'uploads';
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$newname = time();
$dandom = rand(100, 999);
$name = $newname.$random.'.'.$ext;
$query= "UPDATE users SET avatar = '$name' WHERE id=$id";
$res = mysqli_query($dbc, $query);
echo $query.'<br/>';
echo mysqli_error($dbc);
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
$targetFile = $targetPath. $name;
move_uploaded_file($tempFile,$targetFile);
}
?>
users.php
<script>
$(document).ready(fucntion(){
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("#avatar-dropzone");
});
</script>
<form action="uploads.php?id=<?php echo $opened['id']; ?>" class="dropzone" id="avatar-dropzone">
<input type="file" name="file"/>
</form>

uploadify saving results in database

I am new to PHP. here is my code to save results of form to database and uploaded file name.I am using uploadify to upload files.
Here i have 2 problems.
file saves on its location in mozila but do not works for chrome.
coded goes here.
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$picAd= $_FILES ['Filedata']['name'];
// to change same file name
$picAd=rand(0,1000)."_".rand(0,1000)."_".$picAd;
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
// this line works ok for all browsers
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
this line not works for chrome
// $targetFile = str_replace('//','/',$targetPath) . $picAd; <<Error for chrome
if( move_uploaded_file($tempFile,$targetFile)){ // 4
echo true;
}else{
echo false;
}
saving results to database .
static $picAd;
if(isset($_POST['submit'])){
// varibles
$uname=$_REQUEST['userName'];
$pw=$_REQUEST["pw"];
$email= $_REQUEST["email"];
$twit= $_REQUEST["twit"];
$user="user";
if($picAd != ""){
$q="insert into users values(Null,'$user','$uname','$pw','$email','$twit','$picAd')";
mysql_query($q);
mysql_close($dbc); }
}
Here i am not getting the value of $picAd in database its value goes NUll.

Help With Renaming Files With Uploadify

This is my code -
<?php
session_start();
include('connect.php');
mysqli_select_db($connect, "users");
$s = "select * from name where sessionusername = '$u'";
$q = mysqli_query($connect, $s);
$f = mysqli_fetch_array($q);
$name = $f['name'];
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
// $fileTypes = str_replace('*.','',$_REQUEST['fileext']);
// $fileTypes = str_replace(';','|',$fileTypes);
// $typesArray = split('\|',$fileTypes);
// $fileParts = pathinfo($_FILES['Filedata']['name']);
// if (in_array($fileParts['extension'],$typesArray)) {
// Uncomment the following line if you want to make the directory if it doesn't exist
// mkdir(str_replace('//','/',$targetPath), 0755, true);
// Get the extension, and build the file name
//$extension = pathinfo($tempFile, PATHINFO_EXTENSION);
$extension = end(explode(".",$_FILES['Filedata']["name"]));
$new_file_name = '".$name."'".".$extension;
$targetFile = str_replace('//','/',$targetPath) . $new_file_name;
// $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
// } else {
// echo 'Invalid file type.';
// }
}
?>
Why is the above not working? As you can see, I am trying to pull down the name from the users database, and then rename the uploaded file to the name that was pulled from the db.
Can you help me out? Thanks a lot.
Is form enctype == 'multipart/form-data' ?
Ah, now I understand what you're talking about. You should remove your other post. This is an error I've actually encountered with Uploadify before, but I'm not sure what is going on here, specifically. Definitely, checkout your enctype, but for debugging, I implemented this solution for error reporting with Uploadify here: http://www.uploadify.com/forums/discussion/14/upload-script-error-reporting/p1.

Categories