I am new with php web service and trying upload an image using php web service from objective C. Actually, I have to upload an image on server and save its url in mySQL database. But I am unable to do that. Below is my code :
$file_name = substr ( md5(uniqid(rand(),1)), 5, 15);
$uploaddir = 'uploading/';
$file = basename($_FILES['userfile']['name']);
$newname = $file_name . $file;
$uploadfile = $uploaddir . $newname;
$name=$_GET['username'];
$email=$_GET['email'];
$pwd=$_GET['password'];
$phone=$_GET['phone'];
$fb_user=$_GET['facebook_user'];
$fb_pwd=$_GET['facebook_pwd'];
$twitter_user=$_GET['twitter_user'];
$twitter_pwd=$_GET['twitter_pwd'];
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
$query="INSERT INTO `user_master` (`name`,`email`,`password`,`phone`,`facebook_user`,`facebook_pwd`,`twitter_user`,`twitter_pwd`,`followers_no`,`following_no`,`profile_view_no`,`profile_photo`) VALUES ('$name','$email','$pwd','$phone','$fb_user','$fb_pwd','$twitter_user','$twitter_pwd',0,0,0,'$uploadfile')";
if(!mysql_query($query)) die('Error : '.mysql_error());
$id= mysql_insert_id();
mysql_close();
echo json_encode(array("response_code"=>"1","response_message"=> "Successful Transaction","UserId"=>"$id"));
}
else
{
echo json_encode(array("response_code"=>"2","response_message"=> "Please try Again."));
return;
}
The result I am getting is "Plase try again". Any help will be appreciated.
Thanks in advance
Related
Please i need help on this.
I was uploading an image to my server using the move_uploaded_file() method, the method returns true bu the uploaded file was not found inside the specified directory on my server. i have checked thoroughly, searched for the file on the server in case it was uploaded to another directory, checked the permission on the directory but all seems perfect. i really dont know what went wrong .
Here is the snippet of the code.
$uploaddir = './upload_dir/';
$allowed = array('gif', 'png', 'jpg','bmp');
$filename = $_FILES['uploadfile']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$data = array();
if (!in_array($ext, $allowed)) {
$data['status'] = 'error';
$data['error_message'] = 'Invalid file type';
}
else{
$file_name = time() . '_' . uniqid() . '_' . basename($_FILES['uploadfile']['name']);
$file_name = preg_replace('/[^a-z0-9_\.\-]+/i', '_', $file_name);
$file = $uploaddir . $file_name;
if (isset($_FILES['uploadfile']['tmp_name']) && move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
$data['status'] = 'success';
$data['file_name'] = $file_name;
} else {
$data['status'] = 'error';
}
echo json_encode($data);
}
Thanks in advance.
Please use is_uploaded_file($_FILES['uploadfile']['tmp_name']) instead of isset($_FILES['uploadfile']['tmp_name']) for security reason check here
And for the upload dir use $uploaddir = $_SERVER['DOCUMENT_ROOT'] . "/upload_dir/"; instead of $uploaddir = './upload_dir/'; to get absolute path instead of relative one.
thanks everyone. I gave the wrong permission to the folder. that seems to be the problem for my own case.
I was wondering if someone with more experience than me could take a quick second to have a look over my php script for uploading a file to my server.
I had a simple php script that uploaded my image the root of my server when I called the script in my code like so:
http://server.foo.com/images/uploadToDirectory.php
Now I'm trying to amend it so that I can put the name of a folder at the end with the following call:
http://server.foo.com/images/uploadToDirectory.php?dir=test_folder
But for some reason my image is only getting sent to the root of the server. I've checked the logic of my c# code so I think it must be something to do with my php script. Could someone please have a look over it and tell me if I'm doing something silly with my code?
<?
$_SESSION['directory'] = $_POST['directory'];
$uploaddir = './'.$_SESSION['directory'];
$file = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . $file;
print_r($_FILES);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "http://server.ip.address/images/{$file}";
}
else
{
echo "Didn't Work!!!!";
}
?>
Please note, I know this is probably a really bad way for me to go about doing what I want to do, but it's the way I've implemented it. My knowledge of PHP isn't very good.
For comparison here is the code to load to the root of the server:
<?
$uploaddir = './';
$file = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . $file;
print_r($_FILES);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "http://server.ip.address/images/{$file}";
}
else
{
echo "Didn't Work!!!!";
}
?>
Ok so.. if you invoke your script like this:
http://server.foo.com/images/uploadToDirectory.php?dir=test_folder
Then your script should be:
$uploaddir = './'.$_GET["dir"];
That would collect your URL GET variable "dir".
I am trying to upload an image to the server (with a path in mysql table) twice through php with different names. One version of the image as "xxxx.png" and the other version of the image as "xxxxt.png".
My php is:
<?php
if ($_FILES['photo']) {
$target = "images/properties/";
$target = $target . basename( $_FILES['photo']['name']);
$pic = "images/properties/" .(mysql_real_escape_string($_FILES['photo']['name']));
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
mysql_query("INSERT INTO `images` (`productcode`, `photo`) VALUES ('$productcode', '$pic' )");
echo "The new image has been added successfully";
} else {
echo "Error uploading new image - please check the format and size";
}
}
?>
The above code inserts the image into the mysql database and uploads the file to the server correctly.
I am however trying to upload the same image twice with a different naming convention on a "thumbnail" version. The slideshow script in my html only recognises the thumbnails if there are named with a "t" at the end of the filenames hence my problem.
I have been advised to look at the php copy() function to achieve this but am incredibly unclear as to how to incorporate such a function into my existing code.
Happy to provide the html or any other info if required.
Any help much appreciated. I did have another thread attempting to find out the same thing but I wasn't very clear!
Thanks
JD
If I correctly understand you, you do not need to upload this file twice. You already have this file on your server. So you should copy it (optionally do some transitions to make it more like a thumbnail) on your server's hard drive and update database.
Your code should like similar to this:
<?php
if($_FILES['photo'])
{
$target_dir = "images/properties/";
$upload_file_name = basename( $_FILES['photo']['name']);
$upload_file_ext = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);
$target_file = $target_dir . $upload_file_name . '.' . $upload_file_ext;
$target_file_sql = $target_dir . mysql_real_escape_string($upload_file_name . '.' . $upload_file_ext);
$target_thumb = $target_dir . $upload_file_name . 't.' . $upload_file_ext;
$target_thumb_sql = $target_dir . mysql_real_escape_string($upload_file_name . 't.' . $upload_file_ext);
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target_file))
{
mysql_query("INSERT INTO `images` (`productcode`, `photo`) VALUES ('$productcode', '$target_file_sql' )");
echo "The new image has been added successfully";
if (copy($target_file, $target_thumb))
{
mysql_query("INSERT INTO `images` (`productcode`, `photo`) VALUES ('$productcode', '$target_thumb_sql' )");
echo "The new thumb image has been added successfully";
} else
{
echo "Error copying thumb file";
}
} else
{
echo "Error uploading new image - please check the format and size";
}
}
Again, the idea is that you do not need to upload file twice in a row. All you need to do is just copy it on server.
As you've been advised you should use copy(). I didn't fully test this but give it a try:
<?php
if ($_FILES['photo'])
{
$target = "images/properties/";
$ext = array_pop(explode('.', $_FILES['photo']['name']));
$copy = $target . basename($_FILES['photo']['name'], '.' . $ext) . 't.' . $ext;
$target = $target . basename($_FILES['photo']['name']);
$pic = "images/properties/" .(mysql_real_escape_string($_FILES['photo']['name']));
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
copy($target, $copy);
mysql_query("INSERT INTO `images` (`productcode`, `photo`) VALUES ('$productcode', '$pic' )");
echo "The new image has been added successfully";
}
else
{
echo "Error uploading new image - please check the format and size";
}
}
?>
Based on a tutorial I've build an iOS app that uploads images from the device to an web server directory. It worked fine in my localhost, but when I uploaded the files to the server and tested the file weren't copied to the directory I specified! So I suppose in Xcode everything working correctly, the PHP file I using to upload to my server is bellow:
<?php
$connect = mysql_connect("xxxxxxxxx","xxxx","xxxx") or die(mysql_error());
$connectDB = mysql_select_db("xxxxxxxxx");
$insertData = "INSERT INTO feras (nome,vulgo,imagelink) VALUES ('".$_GET['feraname']."','".$_GET['feravulgo']."','http://localhost:8888/ImageUpIPhone/uploads/".$_GET['imagelink'].".jpg')";
$sendData = mysql_query($insertData);
//$file_name = substr ( md5(uniqid(rand(),1)), 5, 15);
$file_name = $_GET['imagelink'];
$uploaddir = 'uploads/';
$file = basename($_FILES['userfile']['name']);
$newname = $file_name . $file;
$uploadfile = $uploaddir . $newname;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
$imagepath = "http://example.com/uploads/{$newname}";
}
print $imagepath;
?>
So, how can I fix that? In my localhost it worked and in the server it doesn't, I can only figure that's about the directory! I using a free host (ueuo.com).
what im trying to do here, get the current file and then upload it,find the extension of the file and rename it! and echo the result!! but it seems wrong, and i dnt know which part!! :((
$fieldname = $_REQUEST['fieldname'];
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES[$fieldname]['name']);
if (move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadfile)) {
//find the extension
$extension= pathinfo($uploadfile);
//rename the file
rename ($uploadfile, "newfile.".$extenion['extension']."");
echo "uploads/newfile.'".$extension['extension']."'"; // "success"
}
I think you should do something like this:
$fieldname = $_POST['fieldname']; // don't use $_REQUEST
$extension = pathinfo($_FILES[$fieldname]['name'], PATHINFO_EXTENSION);
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . 'newfile.'.$extension;
if (move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadfile)) {
// success
}
move_uploaded_file already "renames" the file, there is no need to call rename manually. Just make it in a single operation.
You probably also noticed that I've passed PATHINFO_EXTENSION to pathinfo, since you need only the extension and not the complete path information.
Lastly, I used $_POST instead of $_REQUEST. You shouldn't use $_REQUEST unless you really know what you are doing. This could lead to unexpected variables tampering from cookies or session, per example.
$fieldname = $_POST['fieldname']; #its never going to be a GET! ..not sure y u need this though, the file field should hold the array key, but anyway...
#get the extension by removing everything before the last dot
$extension = preg_replace('#.+\.#', '', $_FILES[$fieldname]['name']);
$newname = "newfile.".$extension;
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . $newname;
if (move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadfile)) {
echo "$uploadfile"; // "success"
}
Untested, but the logic is mo straight to the point.
You should use the new filename as the second argument to move_uploaded_file:
$fieldname = $_REQUEST['fieldname'];
$tmpfile = $_FILES[$fieldname]['tmp_name'];
$info = pathinfo($tmpfile);
$filename = 'newfile.' . $info['extension'];
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . $filename;
if(move_uploaded_file($tmpfile, $uploadfile)) {
echo $uploadfile;
}