# Creating a zip archive with php

**URL:** <https://www.phphelp.com/t/creating-a-zip-archive-with-php/35335>\
**Category:** General PHP Help\
**Created:** [March 11, 2023, 3:35pm UTC](https://www.phphelp.com/t/creating-a-zip-archive-with-php/35335 "2023-03-11T15:35:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [March 11, 2023, 3:35pm UTC](https://www.phphelp.com/t/creating-a-zip-archive-with-php/35335/1 "2023-03-11T15:35:52Z")

</div>

Hello,

The following code creates a zip archive with the complete file  
But it adds reverse slash at the beginning of the files  
What adds this backslash?

 ![Ekran görüntüsü 2023-03-11 183020](https://www.phphelp.com/uploads/default/original/2X/3/3fa6c1c995b0ff460f3cc6af857bf0bd200386da.png)

```auto
$zip = new ZipArchive();
if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
	$source = realpath($source);
	global $zipyorumadizinadi;
	$zip->setArchiveComment($zipyorumadizinadi);
	if (is_dir($source)) {
		$iterator = new RecursiveDirectoryIterator($source);

		$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
		$files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);

		foreach ($files as $file) {
			$file = realpath($file);
			if (is_dir($file)) {
				$zip->addEmptyDir(str_replace($source . '', '', $file . ''));
			} else if (is_file($file)) {
				$zip->addFromString(str_replace($source . '', '', $file), file_get_contents($file));
			}
		}
	} else if (is_file($source)) {
		$zip->addFromString(basename($source), file_get_contents($source));
	}
}

```

---

<div class="post-metadata">

**Author:** ![katsry](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@katsry](https://www.phphelp.com/u/katsry)\
**Post date:** [August 5, 2023, 4:50pm UTC](https://www.phphelp.com/t/creating-a-zip-archive-with-php/35335/2 "2023-08-05T16:50:55Z")

</div>

```auto
$zip = new ZipArchive();
if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
	$source = realpath($source);
	$sourceLength = strlen($source);
	global $zipyorumadizinadi;
	$zip->setArchiveComment($zipyorumadizinadi);
	if (is_dir($source)) {
		$iterator = new RecursiveDirectoryIterator($source);

		$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
		$files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);

		foreach ($files as $file) {
			$file = realpath($file);
			$relativePath = substr($file, $sourceLength);

			if (is_dir($file)) {
				$zip->addEmptyDir(ltrim($relativePath, '/\\'));
			} else if (is_file($file)) {
				$zip->addFromString(ltrim($relativePath, '/\\'), file_get_contents($file));
			}
		}
	} else if (is_file($source)) {
		$zip->addFromString(basename($source), file_get_contents($source));
	}
}

```

It looks like the problem is in the way you are constructing the paths inside the ZIP file. Specifically, when you are using `str_replace($source . '', '', $file . '')` , it’s removing `$source` from `$file` , but it’s not handling the directory separator correctly.

I have used the `ltrim` function is used to remove any leading slashes or backslashes from the relative paths, so they will have the correct format inside the ZIP file.  
Hope this works

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [August 8, 2023, 6:02am UTC](https://www.phphelp.com/t/creating-a-zip-archive-with-php/35335/3 "2023-08-08T06:02:02Z")

</div>

Thank you very much, the problem is fixed

 ![Annotate-a-local-image2](https://www.phphelp.com/uploads/default/original/2X/b/b0d4f68815f13fc870b3a98514ea11fd3dcecd99.png)

---

<div class="post-metadata">

**Author:** ![katsry](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@katsry](https://www.phphelp.com/u/katsry)\
**Post date:** [August 14, 2023, 6:26am UTC](https://www.phphelp.com/t/creating-a-zip-archive-with-php/35335/4 "2023-08-14T06:26:26Z")

</div>

@Adem you are welcome,  
Good day
