Trouble Passing Variables to PHP Script on Image Upload
Try this in Xcode
-(void)postMethod
{
NSString *urlString = [NSString stringWithFormat:@"http:********web-services/register_user.php?firstname=%@&lastname=%@&email=%@&password=%@&location=india&device=IPHONE",details.fname,details.lname,details.emailAddress,details.password];
UIImage *image=details.pic;
NSData *imageData =UIImageJPEGRepresentation(image, 0.1);
double my_time = [[NSDate date] timeIntervalSince1970];
NSString *imageName = [NSString stringWithFormat:@"%d",(int)(my_time)];
NSString *string = [NSString stringWithFormat:@"%@%@%@", @"Content-Disposition: form-data; name=\"profile_pic\"; filename=\"", imageName, @".jpg\"\r\n\""];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:string] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString*s11= [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *responseDictionary1;
responseDictionary1 = [XMLReader dictionaryForXMLString:s11 error:nil];
}
Variable not being passed properly during image upload
You aren't passing a variable with the name recipe_id
to your PHP script - there's no input with that name in your form. Try this:
<form id="<?php echo $recipe_id ?>" action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input name="recipe_id" type="hidden" value="<?php echo $recipe_id ?>">
<button type="submit" name="submit" value="add">Add image!</button>
</form>
How to pass variable to PHP picture
Pass the path of image as get parameter to showImage.php script like.
<?php $pathToPicture = "server/www/images/imagexyz1823014719102714123.png"; ?>
<img src="/resources/php/showImage.php?pathToPicture=<?php echo $pathToPicture;?>" >
Here you can get passed variable from $_GET
array:
<?php
header('Content-Type: image/jpeg');
readfile($_GET['pathToPicture']);
?>
I preferably suggest use of base64_encode and base64_decode for pathToPicture
for this purpose. Also not expose the whole path of your images location openly like this. Have a look at below improved code
<?php $pathToPicture = "imagexyz1823014719102714123.png"; ?>
<img src="/resources/php/showImage.php?pathToPicture=<?php echo base64_encode($pathToPicture);?>" >
<?php
$location = "server/www/images/";
$image = !empty($_GET['pathToPicture']) ? base64_decode($_GET['pathToPicture']) : 'default.jpg';
// In case the image requested doesn't exist.
if (!file_exists($location.$image)) {
$image = 'default.jpg';
}
header('Content-Type: '.exif_imagetype($location.$image));
readfile($location.$image);
?>
Send parameters to another PHP file with image
You can upload the file to a temp location and POST the file's location+name to second.php
file.
For example:
$target_dir = "uploads/";
// If you want unique name for each uploaded file, you can use date and time function and concatenate to the $target_file variable before the basename.
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
// Move the uploaded file
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)
{
// Now you can post the variable $image
$image = $target_file
}
After you query on second.php
you can even do unlink($image);
to delete the file, so the moved images does not eat space on your server.
How to post a javascript variable to a PHP file to assign to a PHP variable?
You wont need ajax for this
First change the input tag to look like below:
<input type="file" onchange="getNewName_one(this.value)" multiple name="file[]" data-maxfilesize="5000000">
Then your create a function in JS:
<script>
function getNewName_one(str) {
var break_path = str.split('\\'); // make sure the \\ is actually two inorder for it to work
var len = break_path.length - 1; // we need to get the last entry of the loop
var filename = break_path[len]; // filename contains only the filename now;
document.cookie = "newImageName_one="+filename;
}
</script>
Then update the php now
<?php
$sentfile = $_COOKIE['newImageName_one'];
?>
If your are having problems with the filename itself; like your are getting some wierd names in the input type="file" just comment it. Thank you.
How to pass image file with variable value in ajax and php
$_POST['message']
is not defined because you didn't put a property called message
in the FormData object. You can't expect something to exist in the POST variables if you didn't submit it to the server in the first place.
You simply need to add it to the form data:
form_data.append('message', message);
Also
url:'insert2.php?'+message
makes no sense (because it doesn't create a valid querystring). Change it to just
url:'insert2.php?'
Related Topics
PHP MySQL Set Connection Timeout
How to Fix Server Status Code: 302 Found by SQL Inject Me Firefox Addon
How to Create a New Page in Prestashop Admin Panel
How to Curry Method Calls in PHP
Fatal Error: Call to Undefined Function MySQL_Connect() in C:\Apache\Htdocs\Test.PHP on Line 2
Understanding PHP "Out of Memory" Error
Issues Porting PHP/Gd Wrapper to Imagick
What Is the Rfc Compliant and Working Regular Expression to Check If a String Is a Valid Url
Jquery $.Post Processing JSON Response
Set Cart Item Price from a Hidden Input Field Custom Price in Woocommerce 3
Php:Difference Between '&&' and 'And'
Insert Data into Database with Codeigniter
Indented List to Multidimensional Array
How to Check If Value Already Exists in MySQL Database
Merge a Table and a Change Log into a View in Postgresql