I am downloading property images from MLS RETS server. When I am using GetObject method to download property images, sometimes Getobject method does not return success parameter then image does not download on local server. Is any solution on it?
Here is my code :
$photos = $rets->GetObject("Property", "Photo", $idata['propertymlsid'], "*", 0);
foreach ($photos as $photo)
{
$imgval="";
$imgval="{$photo['Content-ID']}-{$photo['Object-ID']}.jpg";
if ($photo['Success'] == true)
{
#file_put_contents("photos/{$photo['Content-ID']}-{$photo['Object-ID']}.jpg", $photo['Data']);
#mysql_query("insert into tableName (pro_mlsid,photos_name,image_date)values('".$idata['propertymlsid']."','".$imgval."','".date('Y-m-d h:i:s')."')");
}else
{
// in this section i want to download image. please suggest what to do here? . i have record for this image in database for but could not download it.
}
}
please go through the code. i want to download image in else section of above code.
Unfortunately the RETS protocol is not made for handling images and there are quite a few pitfalls with the whole process.
When an item in the database is deleted the RETS protocol is not able to reflect that change. For listings this is a very rare event but not for images. In either way there is just an error that the requested object has not been found or does not exist. In other words you have to assume that the object was deleted and you have to update your own records.
Images are updated frequently by agents and may have been deleted or changed order.
The image download process is twofold. a) you have to fetch the metadata record first and then b) the image itself with GetObject. However, in the meantime the agent may have deleted the image.
Depending on where you get the data from there may be a lot of latency between the two events. For instance, IDX is usually a secondary database versus access to a RETS feed from the MLS itself.
So bottom line your code is probably okay but the requested image has in fact been deleted since you requested the metadata for that image.
If your process overall works and there's an image missing it may well be gone for good. In theory you should run a second process and try to fetch the actual metadata. If there's no return as well you can safely assume that the record for this image is gone.
Some real estate boards allow agents to upload corrupt photos, or even invalid files (like PDFs). These mistakes made by realtors incorrectly update the RETS feed to indicate a valid photo exists, but when you attempt to download it, it fails.
Simply remove your else statement.
Related
I'm handling a project and from long time that project is in the middle. The problem is : to store and retrive contents from MySQL database (locally in WAMP Server).
The brief overview about the project which inculdes the folowing steps:
1) Users can create account,
2) Upload files (whcih can be text(.rtf & .txt), images (.jpg & .png ), docs (.doc, .xls, .pptx, .pdf ) & videos (.avi & .mp4 ))
3) View those files and
4) Modify them.
So the concept is :
1) User logs-in or do sign-up,
2) He gets options the following options : Text, Images, Docs & Videos.
3) The user choose any option : which opens a new page where user can view his/her existing content in Thumbnail view with its name and
4) There's an option on top of everything to "Add new". Here we've two conditions:
If user clicks on existing, user can view it and delete the existing files. For example, if it's a video, user can play it by clicking on thumbnail.
If user clicks on "Add new", it takes user to another page where user can upload the respective content.
So, the problem is that no one in my team (we're in college, not in a company) knows the queries to store the file into database & after uploading it, retrieve the file's name and thumbnail (only in case of image & video otherwise, a default thumbnail) on previous page.
One of my team member tried to do it, but the uploaded file is shown in every user's account.
So I want to know how we can do it. I mean to say, specific programming code for both problems.
P.S.: If possible please let me know how to set restrictions(validations) of file choosing (like during uploading FB cover, it shows only IMAGES not ALL FILES).
This should help you. But be sure to clean this code from sql injections.
//This is the directory where images will be saved
$target = "pics/";
$target = $target . basename( $_FILES['Filename']['name']);
//This gets all the other information from the form
$Filename=basename( $_FILES['Filename']['name']);
$Description=$_POST['Description'];
//Writes the Filename to the server
if(move_uploaded_file($_FILES['Filename']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['Filename']['name']). " has been uploaded, and your information has been added to the directory";
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("yourdbname") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO tbl_files (Filename,Description)
VALUES ('$Filename', '$Description')") ;
} else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
// Connect to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("dbname") or die(mysql_error()) ;
// directory where images will be moved
$file=mktime()."_".$_FILES['files']['name'];
$target = "images/" . $file;
//This gets all the other information from the form
$description=$_POST['Description'];
//Writes the Filename to the server
if(move_uploaded_file($_FILES['files']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file has been uploaded, and your information has been added to the directory";
//Writes the information to the database
mysql_query("INSERT INTO tbl_files (Filename,Description)
VALUES ('$file', '$description')") ;
} else {
//Gives and error if its not
echo "Sorry, there was a problem uploading the file.";
}
Your question is much too broad. Here are some tips. Come back with more specific questions after you have tried things.
"User" management is a non-trivial task. You need admin page(s) to create new users, delete users, etc. A "end-user" should not be a MySQL "user"; there should be only one or two MySQL users ("app-user"). The PHP code will validate an "end-user", using your own tables for storing their names, passwords, etc. The PHP code will connect to mysql as the app-user.
Media files should be stored on disk as files. This allows the easy use of HTML code to display images (<img...>) or play videos.
Thumbnails -- The simple answer is to treat them like other media. (In one project, I stored them in the db and clumsily converted them to base64 for inclusion in <img...>.)
Thumbnails -- There may be a thumbnail in the "exif data". But I would not trust it to exist. Instead, I would use image* routines in PHP to create a thumbnail from an image. (I do not know how to deal with thumbnails of videos.)
PHP has functions for uploading media. It is a bit clumsy and contorted; study the manual.
PHP is picky (for good security reasons) about access to the filesystem. You will need to poke holes (see open_basedir) to allow access.
You will need to pass information from one page to another. Keep in mind that HTML pages are "stateless", so nothing from one page is implicitly available to the next. You may be able to put everything in the url (...?user=123&img=abc...). But there could be security considerations. Or you may need to get "cookies" involved.
"The user clicks..." -- You may need JavaScript starting with OnClick=DoSomeFunction(...). You may need AJAX, but it does not sound like it.
Otherwise, HTML, PHP, and MySQL can handle everything you need.
Those notes probably encompass several thousand lines of PHP, including a several dozen SQL queries. Divvy up the project among your team so that not everyone has to be come versed in every aspect. Suggested breakdown: User management; Media processing; Flow of pages (clicks, passing args, security, etc); Database abstraction layer.
I'm trying to build an online storage site similar to Google drive. My code can upload files and save it in a specific given path. It also has code for creating files. the only thing is missing is showing the user "how many" or "which" files he/she uploaded in the specific path.
As shown in the Google drive image (in the link below), I want my code to look like similar to this, I want my user to see he/she's uploaded files in a serial on the web page. I'm not sure how to do it or whether it is possible in php or not, or if it is possible in php then which function is needed. I'm also not sure "what to type" in the Google search to find some examples of this type of coding. please tell me anything that will help me to start this part of my project.
http://www.google.com.bd/imgres?sa=X&biw=1024&bih=605&tbm=isch&tbnid=kSX8G1DGHuYiHM:&imgrefurl=http://www.zdnet.com/blog/networking/free-storage-for-you-google-drive-to-arrive-today/2293&docid=VGLnSQuNf4vGLM&imgurl=http://www.zdnet.com/i/story/62/58/002293/google-drive.png&w=1012&h=725&ei=JPCNUtj_FoKtrAfYkoHwCA&zoom=1&ved=1t:3588,r:12,s:0,i:122&iact=rc&page=2&tbnh=173&tbnw=241&start=8&ndsp=14&tx=182&ty=107
(I'm still working offline. I haven't yet launched it online.)
if you need any code from my existing work please tell me. I would love to upload my code.
---thanks.
So, you can just insert records in the Database while uploading (saving/moving files).
And example pseudocode, as you didn't provide any, would be:
if (checkForErrors()) {
// your error page for file exists, improper extension, and other violated constraints
} else {
move_uploaded_file($_FILES['file']['tmp_name'], 'your/path/here/' . $_FILES['file']['name']);
$db->query("INSERT INTO uploads (uploader, filename, uploaded_on) VALUES ('{$_SESSION['username']}', '{$_FILES['file']['name']}', NOW());");
}
// later you can just fetch the results for the current uploaded
$db->query("SELECT filename, uploaded_on FROM uploads WHERE uploader = '{$_SESSION['username']}';");
// under the row of filename will be stored the files that the current user has uploaded
Suppose we have the web application which handle create, read, update and delete articles and each article should have gallery of images. I have to make one to one relation between Article and Gallery and one to many relation between Gallery and Media.
HTML5 gives a lot of features like multiupload, so I want to use this excellent http://blueimp.github.io/jQuery-File-Upload/ plugin for that. The problem is how to handle the file upload "in memory" like other form's data?
For example when we show the page for create new article we should be able to fill in article's data fields and select images to upload, next when we click the save button the images should start upload and after that the form should submit. When validation fails the images should be still displayed on the frontend, but on the server-side nothink should be saved.
One of the solutions is create somethink like "create entity session temporary id" before displaying the entire form and that id can be used to create temporary directory for save uploads, so after success saved form these images can be moved to appropriate directory, but how to make the "create entity session temporary id"?
The other solution I think is the "with the edit id" approach, because we can handle the uploads with previously saved gallery id, but sometimes I can't save new blank article with gallery, cause some of the fields should't be empty in db.
For the Rails I saw https://github.com/thoughtbot/paperclip gem which in the Readme says:
Paperclip is intended as an easy file attachment library for Active Record. The intent behind it was to keep setup as easy as possible and to treat files as much like other attributes as possible. This means they aren't saved to their final locations on disk, nor are they deleted if set to nil, until ActiveRecord::Base#save is called.
My question is how it works?
The problem with enabling file uploads on the create mask is that you eventually end up with orphaned files. This is because a user is able to trigger the upload without saving the actual entity. While creating a very own UploadBundle I thought about this problem for a while and came to the conclusion that there is no truly proper solution.
I ended up implementing it like this:
Given the fact that our problem arise from orphaned files, I created an Orphanage which is in charge of managing these files. Uploaded files will first be stored in a separate directory, along with the session_id. This helps distinguishing files across different users. After submitting the form to create the actual entity, you can retrieve the files from the orphanage by using only your session id. If the form was valid you can move the files from the temporary orphanage directory to the final destination of your files.
This method has some pitfalls:
The orphanage directory itself should be cleaned on a regular basis using a cron job or the like.
If a user will upload files and choose not to submit the form, but instead start over with a new form, the newly uploaded files are going to be moved in the same directory. Therefore you will get both the files uploaded the first time and the second time after getting the uploaded files.
This is not the ultimate solution to this problem but more of a workaround. It is in my opinion however cleaner than using temporary entities or session based storage systems.
The mentioned bundle is available on Github and supports both Orphanage and the jQuery File Uploader plugin.
1up-lab/OneupUploaderBundle
I haven't work with the case personaly, but my co-worker had similar conundrum. She used
punkave/symfony2-file-uploader-bundle
It's a bundle that wrapps jQuery File Upload plugin. It is in the early stages and a lot of things are missing, such as event, but we gave it a shot.
That's what we do: in newAction() we create entity, generate unique dir ID, and store the ID in entity (via regular setDirId()). Than we create the form, which contains hidden field dirId.
We are uploading the files to temp dir on server via ajax, not during the submit. Ajax request requires the ID. It stores files in temp_dir/prefix_ID
Than it's quite simple. Form is sent. If form is valid - move files from temp to dest dir. If not - we have the ID, and are able to show the images.
However, we do not save information about individual files in a separate table in the database. Every time we read the contents of the folder that corresponds to our dirId.
I know it's not the solution You are asking for. It's rather a workaround.
I am creating a dynamic image dependent on a lot of information about the logged in user, but with so many users online, the image system is using a lot of the resources my website hosts have allocated to me.
Is there a way to tell the browser to temporarily cache an image? Similar to temporary cookies?
The image contains numbers such as post count and other frequently incrementing numbers that can cause the browser's automatically cached images to re-download the image potentially every page load. I wouldn't mind a little inaccuracy to save a lot of processing for my server
[Edit]
Browsers already do cache content(unless specifically told not to do so), if it detects a change in last modified then it reloads it.
I am taking a guess that your image being generated is either a) being returned to the browser by a specific php script() or b) is being stored temporarily but is being updated and so the browser retrieves the updated version.
[/Edit]
If you are outputting an image based on dynamically generated content you can write this temporarily to a directory and then based on a predefined time period pull the image if its valid.
if(file_exists($file) && filemtime($file) <= time()-60*20) {
// Regenerate image
} else {
// Load image from cache
}
The example above will not regenerate the image for 20 minutes.
Try to look here
http://www.webscalingblog.com/performance/caching-http-headers-cache-control-max-age.html
In other way, you can set up cache-control header in PHP, and display images using script.
I'm working on a PHP application to store some personal information (including photo). I'm storing the image on a specific folder (let's say myapp/images/people/).
After saving a photo my app is redirected to a page showing the information of the specific person (kind of a profile).
If the photo is saved for the first time (no other photo was previously saved for that person) then the photo is shown in the profile. Everything seems to work at this point.
The problem is when I want to change the photo. When I replace a photo my applications keeps showing the old one. I've checked the server and the old photo is gone, there's only the new one (as I need) but the application doesn't show it.
I guess it's something to do with cache.
I've tried by adding the html tag with no cache values, I've tried by adding the same values by using the header() PHP function but nothing.
I also tried by using:
if(file_exists($imagepath))
{
unlink($imagepath);
}
and similar I've used
if(file_exists($imagepath))
{
unlink($imagepath);
clearstatcache();
}
but also nothing.
Can someone help me with this? Any idea about what's going on? The new photo is in the server, the old one is not but the app keeps showing the previous file.
The image is cached in the browser. The best way is to generate a new image name on the server and return the new image in the HTML. Since it is a new image for the browser, it won't get it from the cache and you will see the new image in the application.