WordPress doesn't recognize meta-data - wordpress

I have moved a newly built WordPress site from a sub directory in the remote server, to the root directory. I have a couple of custom post types with meta data associated with them, including images metadata (i.e. file & URL). Obviously I had to remove the sub directory from the URL. I did so with a replace SQL query.
Now wordpress doesn't recognize the meta data. When I write the following code:
$img = get_post_meta($post->ID,"mf_logo",true);
var_dump($img);
I get "bool(false)". I have tried to upload a new image, and it is showing. I then manually changed its URL through MySQL and again it wasn't recognized.
It is important to note that the problem only happens with meta data in the form of array, and not with 'normal' meta-data
Your kind help would be most appreciated.

When using meta data (update_post_meta, get_post_meta...) arrays will be automatically serialized in db :
http://codex.wordpress.org/Function_Reference/update_post_meta
A passed array will be serialized into a string.
And you cannot simply replace strings in a serialized array :
$data = array('key'=>'value');
echo serialize($data);
This will output : a:1:{s:3:"key";s:5:"value";}
If you simply replace key or value with a shorter/longer string, il will break the data :
a:1:{s:3:"key";s:5:"replace";} is incorrect
a:1:{s:3:"key";s:7:"replace";} is correct
You can make a batch to handle this.
But prior to this, do you know you can let wordpress in its own directory and make it accessible from root directory, without breaking links ?
Take a look here : http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory#Using_a_pre-existing_subdirectory_install

Related

passing variables between pages in Drupal 7

I am trying to pass the variables from one page to another in Drupal 7. Since in Drupal 7 we do not create the php file as such the content of the page gets saved as a plain text in DB, no files get created, so GET/POST are out of the solution.
How can I do this?
Content is saved in database, but every content is defined in some content type. And for every content type you can have different template file. Inside that template file you can put your php code reading GET/POST, or what ever.
So, you can use the usual way and read parameters from template, do what ever you want with them.
One way is to use variable_set() to save the value to database and variable_get() to retrieve the value from database.
To save the value:
variable_set('my_variable_unique_id', 'the value to be saved.');
To read the value back:
$myVariable = variable_get('my_variable_unique_id', 'default value in case could not find a saved value for the variable.');

Wordpress custom home encoding error

I have created a custom home.php file inside my template dir of wordpress.
It's okay but the encoding is broken and I do not understand how to fix it.
The page is a simply custom .php page.
Here you can see: http://tomsblog.it/
More info:
- I get the informations from a utf8_general_ci database
- The page is encoded with UTF-8
- The php code is a simple mysql_fetch_object
$result = $mysql->query("SELECT * FROM blogs WHERE id = 5");
$article = mysql_fetch_object($result);
echo $article->title;
Thanks
can you check the database how the data is storing meaning like it is storing correctly if not you have a problem try to replace the data on a tables here is the link may this help you
UPDATE:
check database character set in wp_config.php file ie. in root folder define('DB_CHARSET', 'utf8');

Drupal 7 path_save with a query arguments

I've searched all over for examples of this, but I haven't found any. I'm trying to create an alias for path which includes query arguments, like profile?arg1=113.
It doesn't matter if I provide path_save() with the plain string representation of the path, or if I provide it with url().
url('profile', array('query' => array('arg1' => $uid)))
Either way, ? and = show up as escaped characters on the URL aliases admin page, which naturally means the path can't be found.
How can I keep the ? and = from being escaped?
12/19/12 Edit 1: the larger context is that I'm trying to set up the alias when a Profile2 profile is being saved (i.e., in mymodule_profile2_presave()) - that's when I'll have all the information I need to programmatically set up the alias.
12/19/12 Edit 2: I just realized that the problem isn't on the insert side - the url_alias table actually has unescaped characters in it. The problem is that Drupal doesn't urldecode the path before using it...
12/20/12 Edit 3: Found a solution using Redirect instead of path aliases. Redirect properly decodes the query string!
You cannot attach the query string to the destination of an alias. The code executed from drupal_path_initialize() doesn't handle the query string correctly.
The function contains the following code.
$_GET['q'] = drupal_get_normal_path($_GET['q']);
Suppose that you have "example" as path alias that points to "node/93?uid=1"; that code would set $_GET['q'] to 'node/93?uid=1', while you are expecting $_GET['q'] to get 'node/93', and $_GET['uid'] to be set to 1.
What you could do is implementing hook_inbound_alter() with code similar to the following one.
function mymodule_url_inbound_alter(&$path, $original_path, $path_language) {
list ($path, $query) = explode('?', $path);
$_GET += drupal_get_query_array($query);
}

File upload and read from database

I am using file upload mechanism to upload file for an employee and converting it into byte[] and passing it to varBinary(Max) to store into database.
Now I what I have to do is, if any file is already uploaded for employee, simply read it from table and show file name. I have only one column to store a file and which is of type VarBinary.
Is it possible to get all file information from VarBinary field?
Any other way around, please let me know.
If you're not storing the filename, you can't retrieve it.
(Unless the file itself contains its filename in which case you'd need to parse the blob's contents.)
If the name of the file (and any other data about the file that's not part of the file's byte data) needs to be used later, then you need to save that data as well. I'd recommend adding a column for the file name, perhaps one for its type (mime type or something like that for properly sending it back to the client's browser, etc.) and maybe even one for size so you don't have to calculate that on the fly for each file (useful when displaying a grid of files and not wanting to touch the large blob field in the query that populates the grid).
Try to stay away from using the file name for system-internal identity purposes. It's fine for allowing the users to search for a file by name, select it, etc. But when actually making the request to the server to display the file it's better to use a simple integer primary key from the table to actually identify it. (On a side note, it's probably a good idea to put a unique constraint on the file name column.)
If you also need help displaying the file to the user, you'll probably want to take the approach that's tried and true for displaying images from a database. Basically it involves having a resource (generally an .aspx page, but could just as well be an HttpHandler instead) which accepts the file ID as a query string parameter and outputs the file.
This resource would have no UI (remove everything from the .aspx except the Page directive) and would manually manipulate the response headers (this is where you'd set the content type from the file's type), write the byte stream to the client, and end the response. From the client's perspective, something like ~/MyContent/MyFile.aspx?fileID=123 would be the file. (You can suggest a file name to the browser for saving purposes in the response headers, which you'd probably want to do with the file's stored name.)
There's no shortage of quick tutorials (some several years old, it's been around for a while) on how to do this with images. Just remember that there's essentially no difference from the server's perspective if it's an image or any other kind of file. All the server needs to do is send the type in the response headers and write the file's bytes to the client. How the client handles the file is up to the browser. In the vast majority of cases, the browser will know what to do (display an image, display via a plugin a PDF, save a .doc, etc.).

File uploading: what should be the name of the file to save to?

I am going to add file upload control to my ASP.NET 2.0 web page so that users can upload files. Files will be stored in the server in the folder with the name as of the user. I want to know what is the best option to name the files when saving to server. Needs to consider security, performance, flexibility to handle files etc.
Options I am considering now :
Upload with the same name as of the input file name
Add User Id+Random Number +File name as of the input file name
Create random numbers +Current Time in seconds and save files with that number. Will have one table to map this number with users upload
Anything else? What is the best way?
NEVER EVER use user input for filenames. Don't use the username. User the user id instead (I assume your users have an unique id).
NEVER use the original filename. Use your solution number 3, plus the user id instead of the username.
For your information, PHP had a vulnerability a few years ago: one could forge a HTTP POST request with a file upload, and with a file name like "../../anything.php", and the php _FILES array, supposed to contain sanitized values, didn't detect these kind of file names, so one could write files anywhere in the filesystem.
I'd use a combination of
User ID
A random generated string (e.g. a GUID)
Example PDF file name: 23212-dd503cf8-a548-4584-a0a3-39dc8be618df.pdf
This way, the user can upload as many files as he/she wants, without file name conflict, and you are also able to point out which files belong to which users, just by looking at the file names.
I don't see the need to include any other information in the file name, since upload time/date and such can be retrieved from the file's attributes.
Also, you should store the files in a safe location, which external users, such as visitors of your website, cannot access. Instead, you deliver the file to them through a proxy web page (you read the file from the safe location, and pass the data on to the user). For this solution, a database is needed to keep track of files, their location, etc.
This also makes you able to control which users have access to which files through your code.
Update: Here's a description of how the solution with the proxy web page could be implemented.
Create a Web Form with the name GetFile.aspx
GetFile.aspx takes one query parameter named fileid, which is used to identify the file to get. E.g.: http://www.mypage.com/GetFile.aspx?fileid=100
Use the fileid parameter to lookup the file location in the database, so that it can be read and sent to the user. In the Web Form you use Request.QueryString("fileid") to get the file ID and use it in a query that will look something like this (SQL): SELECT FileLocation FROM UserFiles WHERE FileID = 100
Read the file using a System.IO.FileStream and output its contents through Response.Write. Remember to set the appropriate content type using Response.ContentType first, so that the client browser handles the requested file correctly (see this post on asp.forums.net and the MDSN article which is also referred to in the post, which both discuss a method of determining the appropriate content type automatically).
If you choose this approach, it's easy to implement your own simple security or custom actions later on, such as making sure a user is logged into your web site before you send the file, or that users can only access files they uploaded themselves, or logging which users download which files, etc. The possibilities are endless ;-)
Take a look at the System.IO.Path class as it has lots of useful functions you can utilise, such as:
Check which characters are invalid in a file name:
System.IO.Path.GetInvalidPathChars();
Get a random file name:
System.IO.Path.GetRandomFileName();
Get a unique, randome filename in the temporary directory
System.IO.Path.GetTempFileName();
I would go with option #3. A table mapping these files with users will provide other uses down the road, it always does. If you use the mapping, the only advantage of appending the user name or id to the file is if you are trying to debug a problem.
I'd probably use a GUID instead of a random number but either would work. The important things in my opinion are
No username as part of the filename as any part of the stored file
Never use the original file name as any part of the stored file
Use a random number or GUID to ensure no duplicate file
Adding an user id to the file will help with manual debugging issues
There is more to this than meets the eye...which I am thinking that you already knew!
What sort of files are you talking about? If they are anything even remotely big or in such quantity that the group of files could be big I would immediately suggest that you add some flexibility to your approach.
create a table that stores the root paths to various file stores (this could be drives, unc paths, what ever your environment supports). It will initially have one entry in it which will be your first storage location. An nice attribute to maintain with this data is how much room can be stored here.
maintain a table of file related data (id {guid}, create date, foreign key to path data, file size)
write the file to a root that still has room on it (query all file sizes stored in a root location and compare to that roots capacity)
write the file using a GUID for the name (obfuscates the file on the file system)..can be written without the file extension if security requires it (sensitive files)
write the file according to its create date starting from the root/year{number}/month{number}/day{number}/file.extension
With a system of this nature in place - even though you won't/don't need it up front - you can now more easily relocate the files. You can better manage the files. You can better manage collections of files. Etc. I have used this system before and found it to be quite flexible. Dealing with files that are stored to a file system but managed from a database can get a bit out of control once the file store becomes so large and things need to get moved around a bit. Also, at least in the case of windows...storing zillions of files in one directory is usually not a good idea (the reason for breaking things up by their create date).
This complexity is only really needed when you have high volumes and large foot prints.

Resources