Pregunta

He leído este tutorial sobre el almacenamiento de imágenes en el PP. En el tutorial, el autor se escapa caracteres especiales en los datos binarios antes de insertar: http: / /www.phpriot.com/articles/images-in-mysql/7 (usando addslashes aunque mysql_real_escape_string es preferible - pero eso es otro tema)

.

El punto es, cuando se muestran, sólo muestra los datos, ya que se almacena: http://www.phpriot.com/articles/images-in-mysql/8

Mis preguntas:

1) ¿Necesitamos escapar caracteres especiales, incluso para el tipo de campo binario (blob)?

2) Si es así, entonces, ¿no se debe a "unescape" los caracteres de nuevo con el fin de mostrar la imagen correctamente? (Si es así, ¿cuál es la mejor manera de hacerlo Cualquier comentario acerca de la eficiencia Para las grandes imágenes:.?? Escapando y unescape puede ser un gran sobrecarga).

O es que mi comprensión acerca de escapar es totalmente erróneo (y escapar sólo afecta a la consulta y no inserta los datos finales / almacenado?).

gracias

JP

¿Fue útil?

Solución

Your understanding of escaping is wrong. The data being inserted into the database is escaped, so that the query parser sees the information as intended.

Take the string "Jean-Luc 'Earl Grey' Picard". Escaping results in: 'Jean-Luc \'Earl Grey\' Picard'

When MySQL receives this, it understands that the escaped quotes need to be taken literally, that is what escaping means, and will store them in the database. It will not store the escape-characters in the database. The \ indicates to MySQL that it should take the character following it literally.

When retrieving, the data is presented to your application without the escaping characters, as they are removed when parsing the query.

Otros consejos

1) Do we need to escape special characters even for binary field type (blob)?

Yes, because mysql_real_escape_string() (which is indeed the one to use) provides protection against SQL injection attacks, which could easily be inside an image file as well. Any arbitrary data you feed into a database must be sanitized first.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top